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: 
Short oneliner for CamelCases when you have spaces also included (doesn't work correctly if you have a word inbetween with small starting-letter):

```
a = "Test String"
a.gsub(' ', '').underscore

  => "test_string"

```

**EDIT:** As pointed out by @dft then this method is not part of Ruby but Rails.
The ruby core itself has no support to convert a string from (upper) camel case to (also known as pascal case) to underscore (also known as snake case).

So you need either to make your own implementation or use an existing gem.

There is a small ruby gem called **lucky\_case** which allows you to convert a string from any of the 10+ supported cases to another case easily:

```
require 'lucky_case'

# convert to snake case string
LuckyCase.snake_case('CamelCaseString')      # => 'camel_case_string'
# or the opposite way
LuckyCase.pascal_case('camel_case_string')   # => 'CamelCaseString'

```

You can even monkey patch the String class if you want to:

```
require 'lucky_case/string'

'CamelCaseString'.snake_case  # => 'camel_case_string'
'CamelCaseString'.snake_case! # => 'camel_case_string' and overwriting original

```

Have a look at the offical repository for more examples and documentation:

<https://github.com/magynhard/lucky_case>