Underscoming a class

A

Allen Walker

If I have a String like "DistributionMethod" is there any helper out
there that will turn it into "distribution_method" ?
 
T

Todd Benson

If I have a String like "DistributionMethod" is there any helper out
there that will turn it into "distribution_method" ?

I'm sure there's one out there, but for simple cases...

s = "SomeCamelCase"
s.scan(/.*?[A-Z].*?[^A-Z]*?/).map {|w| w.downcase}.join("_")

Todd
 
T

Trans

If I have a String like "DistributionMethod" =A0is there any helper out
there that will turn it into "distribution_method" ?

Check out the English library (require 'english/style').

http://english.rubyforge.org

Also, Facets and ActiveSupport have such methods.

The method names to look for are #snakecase, #underscore, and
#methodize.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top