I want to turn string into CamelCase fashion In Ruby. The question also applies to words with underscores.
For example:
"human" => "Human"
"little_human" => "LittleHuman"
How can I do this?
I want to turn string into CamelCase fashion In Ruby. The question also applies to words with underscores.
For example:
"human" => "Human"
"little_human" => "LittleHuman"
How can I do this?
With regexp:
def camelize(str)
str.gsub(/(^.)|(_.)/) { |l| l[-1].upcase }
end
In rails there is a camelize
method. In ruby you can write the method on your own. Something like
def camelize(s)
s.downcase.split('_').map(&:capitalize).join
end