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: 
[Rails' ActiveSupport](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore) 
adds underscore to the String using the following: 

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

```

Then you can do fun stuff: 

```
"CamelCase".underscore
=> "camel_case"

```
I had trouble running `"CamelCaseString".underscore` in a rake task. This helped me:

```
ActiveSupport::Inflector.underscore "CamelCaseString"
=> "camel_case_string"

```

Of course, you need to require ActiveSupport