Created
June 15, 2012 05:33
-
-
Save tomdepplito/2934851 to your computer and use it in GitHub Desktop.
This class creates a book with a user-generated title. The title method will then take the lowercase title string and capitalize only the important words.
This file contains hidden or 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 Book | |
attr_accessor :title | |
def title | |
low_words = ['the', 'in', 'a', 'an', 'and' ] | |
array_words = @title.split | |
first_word = array_words[0].split(//) | |
first_word[0].upcase! | |
array_words[0] = first_word.join | |
joined_words = [] | |
array_words.each do |word| | |
if !low_words.include?(word) | |
char_array = word.split(//) | |
char_array[0].upcase! | |
joined_words << char_array.join | |
else | |
joined_words << word | |
end | |
end | |
joined_words.join(" ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment