Question: Is there any ready function which converts camel case Strings into underscore separated string?

I want something like this:

```
"CamelCaseString".to_underscore      

```

to return "camel\_case\_string".

...

Answer: 
Here's how [Rails does it](http://rails.rubyonrails.org/classes/Inflector.html#M001631):

```
   def underscore(camel_cased_word)
     camel_cased_word.to_s.gsub(/::/, '/').
       gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
       gsub(/([a-z\d])([A-Z])/,'\1_\2').
       tr("-", "_").
       downcase
   end

```
Receiver converted to snake case: <http://rubydoc.info/gems/extlib/0.9.15/String#snake_case-instance_method>

This is the Support library for DataMapper and Merb. (<http://rubygems.org/gems/extlib>)

```
def snake_case
  return downcase if match(/\A[A-Z]+\z/)
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
  gsub(/([a-z])([A-Z])/, '\1_\2').
  downcase
end

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

```