If you use ActiveSupport
(for instance because of Rails or any other dependency), then have a look at the ActiveSupport::Inflector
module. These methods are immediately available to you for any String
.
'egg_and_hams'.classify # => "EggAndHam"
'posts'.classify # => "Post"
Keep in mind that the standard separator in Ruby is the _
, not the -
. It means you probably need to replace it.
'my-name'.tr('-', '_').classify
=> "MyName"
'my-name'.tr('-', '_').camelize(:lower)
=> "myName"
Using ActiveSupport is just delegating the job. Keep in mind that, behind the scenes, these conversions in Ruby are very likely to be performed using regular expressions.
In fact, in Ruby regexp are cheap and very common.