Here is another solution. It's not so pretty, but it's about abbreviations that you want to stay in caps and abbreviations that you don't want to be crippled like my previous use of the abbreviation. In addition to this, it ensures that your first and last word is capitalized.
class String def titlecase lowerCaseWords = ["a", "aboard", "about", "above", "across", "after", "against", "along", "amid", "among", "an", "and", "around", "as", "at", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "concerning", "considering", "d", "despite", "down", "during", "em", "except", "excepting", "excluding", "following", "for", "from", "in", "inside", "into", "it", "ll", "m", "minus", "near", "nor", "of", "off", "on", "onto", "opposite", "or", "outside", "over", "past", "per", "plus", "re", "regarding", "round", "s", "save", "since", "t", "than", "the", "through", "to", "toward", "towards", "under", "underneath", "unlike", "until", "up", "upon", "ve", "versus", "via", "with", "within", "without", "yet"] titleWords = self.gsub( /\w+/ ) titleWords.each_with_index do | titleWord, i | if i != 0 && i != titleWords.count - 1 && lowerCaseWords.include?( titleWord.downcase ) titleWord else titleWord[ 0 ].upcase + titleWord[ 1, titleWord.length - 1 ] end end end end
Here are some examples of use
puts 'barack obama'.titlecase