Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
Created November 24, 2014 15:11
Show Gist options
  • Save eladmeidar/5b16fa2304c507332e06 to your computer and use it in GitHub Desktop.
Save eladmeidar/5b16fa2304c507332e06 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 index == 0 || !articles.include?(word)
title_array << word.capitalize
else
title_array << word
end
end
title_array.join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment