Created
November 24, 2014 15:11
-
-
Save eladmeidar/5b16fa2304c507332e06 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 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