Skip to content

Instantly share code, notes, and snippets.

@peat
Last active December 23, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peat/6617458 to your computer and use it in GitHub Desktop.
Save peat/6617458 to your computer and use it in GitHub Desktop.
SMALL_WORDS = %w{a an and as at but by en for if in of on or the to v v. via vs vs.}
def titleize( sentence )
capped_words = sentence.downcase.split(" ").map do |word|
SMALL_WORDS.include?(word) ? word : word.capitalize
end
capped_words[0] = capped_words[0].capitalize unless capped_words.empty?
capped_words.join(" ")
end
puts titleize("The quick brown fox jumps over the lazy dog")
puts titleize("the quick brown fox jumps over a lazy dog")
puts titleize("a quick brown fox jumps over the lazy dog")
puts titleize("A QUICK BROWN FOX JUMPS OVER THE LAZY DOG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment