# Name conversion: search terms?

**URL:** <https://www.ruby-forum.com/t/name-conversion-search-terms/76830>\
**Category:** Ruby\
**Created:** [November 21, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/name-conversion-search-terms/76830 "2006-11-21T15:55:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugh\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@Hugh\_S](https://www.ruby-forum.com/u/Hugh_S)\
**Post date:** [November 21, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/name-conversion-search-terms/76830/1 "2006-11-21T15:55:24Z")

</div>

I’m doing some code generation and need to convert from CamelCase to  
whatever\_this\_is\_called. Wikipedia has a page for CamelCase but not  
underscore separated words.

I’m thinking that someone must have done this already, and got the  
edge cases sorted out, so rather than coming up with a wheel with an  
iron tyre I’d search… for what? Is there a single correct term  
for how we name methods in Ruby?

```
    Thank you,
    Hugh
```

---

<div class="post-metadata">

**Author:** ![Hugh\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@Hugh\_S](https://www.ruby-forum.com/u/Hugh_S)\
**Post date:** [November 21, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/name-conversion-search-terms/76830/2 "2006-11-21T16:05:25Z")

</div>

Hugh S. a écrit :

> ```
> Hugh
> 
> ```

Hi,

Ruby on Rails have similar things. You should take a look at Active  
Support, and particulary the  
ActiveSupport::CoreExtensions::String::Inflections class. The  
“underscore” method can be used to transform a string from CamelCase to  
underscore\_seprated\_words.

---

<div class="post-metadata">

**Author:** ![Hugh\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@Hugh\_S](https://www.ruby-forum.com/u/Hugh_S)\
**Post date:** [November 22, 2006, 12:45am UTC](https://www.ruby-forum.com/t/name-conversion-search-terms/76830/3 "2006-11-22T00:45:27Z")

</div>

On Nov 21, 2006, at 6:54 AM, Hugh S. wrote:

> ```
> Hugh
> 
> ```

class String

# “FooBar”.snake\_case #=\> “foo\_bar”

def snake\_case  
return self unless self =~ %r/[A-Z]/  
self.reverse.scan(%r/[A-Z]+|[^A-Z]\*[A-Z]+?/).reverse.map{|word|  
word.reverse.downcase}.join ‘\_’  
end

# “foo\_bar”.camel\_case #=\> “FooBar”

def camel\_case  
return self if self =~ %r/[A-Z]/ and self !~ %r/_/  
words = self.strip.split %r/\s\*_+\s\*/  
words.map!{|w| w.downcase.sub(%r/^./){|c| c.upcase}}  
words.join  
end

end

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![Hugh\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/d2c977/32.png) [@Hugh\_S](https://www.ruby-forum.com/u/Hugh_S)\
**Post date:** [November 21, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/name-conversion-search-terms/76830/4 "2006-11-21T16:13:27Z")

</div>

On Wed, 22 Nov 2006, Bruno M. wrote:

> Hi,
> 
> Ruby on Rails have similar things. You should take a look at Active Support,  
> and particulary the ActiveSupport::CoreExtensions::String::Inflections class.  
> The “underscore” method can be used to transform a string from CamelCase to  
> underscore\_seprated\_words.

Thank you. That’s splendid.

> –  
> Bruno M.

```
    Hugh
```
