Billy Logan Billy Logan - 6 months ago 17
Ruby Question

How to convert string to UpperCamelCase in Ruby?

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?

Answer

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
Comments