Skip to content

Instantly share code, notes, and snippets.

@andyjbas
Created January 21, 2013 17:33
Show Gist options
  • Save andyjbas/4587688 to your computer and use it in GitHub Desktop.
Save andyjbas/4587688 to your computer and use it in GitHub Desktop.
class Book
def initialize
end
attr_reader :title
def title=(string)
@title = string.titlize
end
end
class String
def titlize
array = self.split
array[1..-1].collect! { |s| s.selective_caps }
array.first.capitalize!
array.join(" ")
end
def selective_caps
small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.)
return self if small_words.include?(self)
self.capitalize!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment