Last Updated: February 25, 2016
·
788
· douo

one line to camelize underscore in Ruby

"foo_bar_zoo".split('_').to_enum.with_index.map{|s,i| i==0?s:s.capitalize}.reduce(:+)

and this, convenience for repeatedly.

loop do
  puts gets.split('_').to_enum.with_index.map{|s,i| i==0 s:s.capitalize}.reduce(:+)
end

2 Responses
Add your response

"foo_bar_zoo".gsub(/_(\w)/) { $1.upcase }

Well, it's easier with little bit of regexp magic :)

over 1 year ago ·
over 1 year ago ·