Last active
August 29, 2015 14:08
-
-
Save xander-miller/c8f411b5681607c52333 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
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