Skip to content

Instantly share code, notes, and snippets.

@beaugaines
Last active December 26, 2015 19:29
Show Gist options
  • Save beaugaines/7201433 to your computer and use it in GitHub Desktop.
Save beaugaines/7201433 to your computer and use it in GitHub Desktop.
class String
def camel_case
parts = self.split
parts.each { |p| p.capitalize! }
parts[0].downcase!
parts.join
end
def camel_case!
parts = self.split
parts.each { |p| p.capitalize! }
parts[0].downcase!
replace(parts.join)
end
# another way
def camel_case
self.downcase.split.each_with_index do |value, i|
value.capitalize! unless i == 0
end.join
end
def titleize
small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.)
words = self.downcase.split
words.first.capitalize!
words.each do |word|
word.capitalize unless small_words.include?(word)
end.join(' ')
end
end
class Hash
def to_array
self.to_a.flatten.map { |word| word.to_s }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment