Skip to content

Instantly share code, notes, and snippets.

@ivorpad
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save ivorpad/84f2e12570b94e55c4eb to your computer and use it in GitHub Desktop.

Select an option

Save ivorpad/84f2e12570b94e55c4eb to your computer and use it in GitHub Desktop.
Loops: Title Case - Bloc.io: Full Stack
class Title
attr_reader :str
def initialize(str)
@str = str
end
def fix
common_words = ["the", "of", "a", "on", "in", "for", "by"]
fixed = []
str.downcase.split.each_with_index do |word, index|
if index > 0 && common_words.include?(word)
fixed << word
else
fixed << word.capitalize
end
end
fixed.join(" ")
end
end
my_title = Title.new("a title OF A book")
p my_title.fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment