0

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?

Billy Logan
  • 2,470
  • 6
  • 27
  • 45

2 Answers2

3

With regexp:

def camelize(str)
  str.gsub(/(^.)|(_.)/) { |l| l[-1].upcase }
end
Sergii K
  • 845
  • 9
  • 16
2

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
Ursus
  • 29,643
  • 3
  • 33
  • 50