Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created June 19, 2012 02:41
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 kmandreza/2952007 to your computer and use it in GitHub Desktop.
Save kmandreza/2952007 to your computer and use it in GitHub Desktop.
Book Class
def title_case(string)
first_word = true
stop_words = ["a", "an", "the", "and"]
words = string.split
words.each do |word|
is_stop_word = stop_words.include?(word)
is_i = word == "i"
if (first_word || !is_stop_word || is_i)
word[0] = word[0].upcase
end
word[1..word.length-1] = word[1..word.length-1].downcase
first_word = false
end
words.join(" ")
end
puts title_case("my big fat screen")
puts title_case("mY big fAt SCreen")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment