The method is supposed to take in a name of a book and return it in proper title case. All of my specs pass ( )handles non-letter characters, handles upper and mixed cases) except the last one which is to return special words like McDuff or McComb with a capital 3rd letter. Anyone see what I'm doing wrong? And, is there a way to simplify this, using the tools at hand and not some higher level shortcut?
class String
define_method(:title_case) do
sentence_array = self.downcase.split
no_caps = ["a", "an", "the", "at", "by", "and", "as", "but", "or", "for", "in", "nor", "on", "at", "up", "to", "on", "of", "from", "by"]
sentence_array.each do |word|
if no_caps.include?(word)
word
else
word.capitalize!
end
sentence_array.first.capitalize!
# Manage special words
if (word.include?("mc"))
letter_array = word.split!("") # word with mc changed to an array of letters
if (letter_array[0] == "m") && (letter_array[1] == "c") # 1st & 2nd letters
letter_array[2].capitalize!
word = letter_array.join
end
end
end
sentence_array.join(" ")
end
end