Skip to content

Instantly share code, notes, and snippets.

@xander-miller
Last active August 29, 2015 14:08
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 xander-miller/c8f411b5681607c52333 to your computer and use it in GitHub Desktop.
Save xander-miller/c8f411b5681607c52333 to your computer and use it in GitHub Desktop.
class Title
attr_reader :string
def initialize(string)
@string = string
end
def fix
# A neat Ruby trick for making an array of strings
# Equivalent to: ['a', 'and', 'the', 'of']
articles = %w{a and the of}
word_array = string.downcase.split(" ")
title_array = []
word_array.each_with_index do |word, index|
if articles.include?(word)
title_array << word
else
title_array << word.capitalize
end
end
title_array.first.capitalize
title_array.join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment