Last active
December 26, 2015 19:29
-
-
Save beaugaines/7201433 to your computer and use it in GitHub Desktop.
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 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