Last active
December 23, 2015 09:49
-
-
Save peat/6617458 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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