I am trying to convert a name from snake case to camel case. Are there any built-in methods?
Eg: \"app_user\"
to \"AppUser\"
(I hav
Source: http://rubydoc.info/gems/extlib/0.9.15/String#camel_case-instance_method
For learning purpose:
class String
def camel_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map{|e| e.capitalize}.join
end
end
"foo_bar".camel_case #=> "FooBar"
And for the lowerCase variant:
class String
def camel_case_lower
self.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end
end
"foo_bar".camel_case_lower #=> "fooBar"