Skip to content

Instantly share code, notes, and snippets.

@tomdepplito
Created June 15, 2012 05:33
Show Gist options
  • Save tomdepplito/2934851 to your computer and use it in GitHub Desktop.
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.
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