# Enumeration with symbol name information

**URL:** <https://www.ruby-forum.com/t/enumeration-with-symbol-name-information/109930>\
**Category:** Ruby\
**Created:** [July 20, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/enumeration-with-symbol-name-information/109930 "2007-07-20T12:04:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/enumeration-with-symbol-name-information/109930/1 "2007-07-20T12:04:26Z")

</div>

Hello everybody…  
I have a beginne’s question:

I’ve created class for my numeric constants, it looks like:

class MyClass

NAME1 = 0  
NAME2 = 1  
NAME3 = 2  
#etc…

def self.to\_s(something)  
case something  
when 0  
“NAME1”  
when 1  
“NAME2”  
when 2  
“NAME3”  
#etc…  
end  
end

end

What is more elegant way to do this? Basically, I want to convert the  
numerical value to string representation (ideally via identifier name).

Thanks in advance,  
Sasa

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/enumeration-with-symbol-name-information/109930/2 "2007-07-20T12:36:33Z")

</div>

On 7/20/07, Sasa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NAME3 = 2  
> #etc…  
> end  
> end
> 
> end
> 
> What is more elegant way to do this? Basically, I want to convert the  
> numerical value to string representation (ideally via identifier name).

In ruby we often use symbols in place of of enumerations. Any place  
you’d  
use NAME0, use :name0. For NAME1 use :name1, etc.

For example:  
class MyClass  
def something  
case @enum\_value  
when :name0  
puts “In name zero mode”  
when :name1  
puts “In name one mode”  
end  
end  
end

:name0.to\_s #=\> “name0”

If, for whatever reason you don’t want to use symbols, a way I  
occasionally  
use to emulate enums is the following:

NAME0 = Object.new

def NAME0.to\_s  
“NAME0”  
end

NAME1 = Object.new

def NAME1.to\_s  
“NAME1”  
end

…

Another method that could be used to simplify your existing code is to  
use a  
hash:

NAME0 = 0  
NAME1 = 1  
NAME2 = 2

Names = { NAME0 =\> “NAME0”, NAME1 =\> “NAME1”, NAME2 =\> “NAME2” }

def self.to\_s(something)  
Names[something]  
end

You can even wrap this one up (or the previous method with some  
metaprogramming:)

def self.enum(\*args)  
const\_set(“Names”, {})  
args.each\_with\_index do |v, i|  
Names[i] = v  
const\_set(v, i)  
end  
end

enum “NAME0”, “NAME1”, “NAME2”

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/enumeration-with-symbol-name-information/109930/3 "2007-07-20T13:19:01Z")

</div>

On 2007-07-20 19:00:04 +0900 (Fri, Jul), Sasa wrote:

> #etc…  
> end  
> end
> 
> end
> 
> What is more elegant way to do this? Basically, I want to convert the  
> numerical value to string representation (ideally via identifier name).
> 
> Thanks in advance,  
> Sasa

If your numeric values always start from 0, then you may use an array:

class MyClass  
NAMES = [‘NAME1’,‘NAME2’,‘NAME3’]

def self.to\_s(something) # personally I would rather avoid redefining  
class.to\_s  
NAMES[something.to\_i] || default\_value  
end  
end

If this is unacceptable, you may use a hash:

NAMES = { 1 =\> ‘NAME1’, 2 =\> ‘NAME2’, 5 =\> ‘NAME3’ }  
NUMBERS = Hash[\*NAMES.map {|k,v| [v,k]}.flatten ] # may be useful  
🙂

def self.to\_s(something)  
NAMES[something] || defaul\_value  
end

def self.to\_i(something) # may be useful 🙂  
NUMBERS[something] || default\_number  
end
