I am supposed to:
Capitalize the first letter of string.
Capitalize every word except articles (the, a, an), conjunctions (and), and prepositions (in).
Capitalize i (as in "I am male.").
Specify the first word of a string (I actually have no idea what this means. I'm trying to run the spec file to test other functions).
Here's my code:
class Book
def initialize(string)
title(string)
end
def title(string)
arts_conjs_preps = %w{ a an the and
but or nor for
yet so although
because since
unless despite
in to
}
array = string.downcase.split
array.each do |word|
if (word == array[0] || word == "i") then word = word.capitalize
if arts_conjs_preps !include?(word) then word = word.capitalize
end
puts array.join(' ')
end
end
puts Book.new("inferno")
Ruby says I'm messing up at:
puts Book.new("inferno") <--(right after the last line of code)
I get exactly the same error message with this test code:
def title(string)
array = string.downcase.split
array.each do |word|
if word == array[0] then word = word.capitalize
end
array.join(' ')
end
puts title("dante's inferno")
The only other Stack Overflow thread regarding this particular syntax error that did not suggest trailing or missing ends or .s as the root of the problem is here. The last comment recommends deleting and recreating the gemset, Which sounds scary. And I'm not sure how to do.
Any thoughts? Simple solution? Resources to help?
Solution
class Book
def initialize(string)
title(string)
end
def title(string)
arts_conjs_preps = %w{ a an the and
but or nor for
yet so although
because since
unless despite
of in to
}
array = string.downcase.split
title = array.map do |word|
if (word == array[0] || word == "i") || !arts_conjs_preps.include?(word)
word = word.capitalize
else
word
end
end
puts title.join(' ')
end
end
Book.new("dante's the inferno")