RUBY do loop and if statements not executing correctly -
i novice ruby coder completing homework exercise related building loops. rspec criteria problem shown below. have tried number approaches.
i have been able pass first 2 conditions totally stuck on how match , transform english articles (a, an, the). doesn't appear correctly looping , using if construct correctly within method.
can suggest ideas can approach solve or if there simple syntax issue missing?
describe "title" describe "fix" "capitalizes first letter of each word" expect( title.new("the great gatsby").fix ).to eq("the great gatsby") end "works words mixed cases" expect( title.new("little red riding hood").fix ).to eq("little red riding hood") end "downcases articles" expect( title.new("the lord of rings").fix ).to eq("the lord of rings") expect( title.new("the sword , stone").fix ).to eq("the sword , stone") expect( title.new("the portrait of lady").fix ).to eq("the portrait of lady") end "works strings uppercase characters" expect( title.new("the sword , stone").fix ).to eq("the sword , stone") end end
here code have come far:
class title def initialize(book) @book=book end def fix #create array of articles art = ['a','an','the'] #reformat string lower case reset common format @book.downcase.split(' ').map {|w| w.capitalize }.join(' ') #resplit oll lower case string array arr = @book.split(' ') #for each element of array check if in art array if tru arr.each_with_index |element, index| if art.include?(element) element.downcase elsif index == 1 element.upcase else element.upcase end end arr.join(' ') end end
here error messages receiving:
• rspec::expectations::expectationnotmeterror expected: "the great gatsby" got: ["the", "great", "gatsby"] (compared using ==) exercise_spec.rb:6:in `block (3 levels) in <top (required)>' • title fix works words mixed cases rspec::expectations::expectationnotmeterror expected: "little red riding hood" got: ["little", "red", "riding", "hood"] (compared using ==) exercise_spec.rb:9:in `block (3 levels) in <top (required)>' • title fix downcases articles rspec::expectations::expectationnotmeterror expected: "the lord of rings" got: ["the", "lord", "of", "the", "rings"] (compared using ==) exercise_spec.rb:12:in `block (3 levels) in <top (required)>' • title fix works strings uppercase characters rspec::expectations::expectationnotmeterror expected: "the sword , stone" got: ["the", "sword", "and", "the", "stone"] (compared using ==) exercise_spec.rb:17:in `block (3 levels) in <top (required)>'
there several issues here.
- in lot of places not assign variables.. example,
element.upcase
, not assignelement.upcase
element
. needelement = element.upcase
, orelement.upcase!
- speaking of
upcase
..upcase
capitalizing entire word, want usecapitalize
(orcapitalize!
) - ruby arrays 0 indexed, when checking first word want use index == 0
- the order of if/else's isn't quite right. right first word check if article (and if don't capitalize). never check if first word after that.
- you're missing 'and' in articles array
Comments
Post a Comment