# Simple Enumerations in Rub

**URL:** <https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968>\
**Category:** Ruby\
**Created:** [April 16, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968 "2006-04-16T21:55:32Z")\
**Posts on this page:** 6\
**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:** [April 16, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/1 "2006-04-16T21:55:32Z")

</div>

Hi all,

Recently I’ve had to come up with a way to simulate an enumerated type  
for a Rails project that I am working on. I looked around for a bit to  
see what is out there and what people are doing now, but nothing I found  
would satisfy all of my design requirements. I wrote up a blog entry on  
the subject which discusses what our requirements were and how we  
addressed them. The results are here:

[http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby](http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby)

I would greatly appreciate feedback from the Ruby community!

Thanks,  
rusty

---

<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:** [April 16, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/2 "2006-04-16T23:21:07Z")

</div>

Rusty Geldmacher wrote:

> Recently I’ve had to come up with a way to simulate an enumerated type  
> for a Rails project that I am working on. I looked around for a bit to  
> see what is out there and what people are doing now, but nothing I found  
> would satisfy all of my design requirements. I wrote up a blog entry on  
> the subject which discusses what our requirements were and how we  
> addressed them. The results are here:
> 
> [http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby](http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby)

Been there, done that. See attachment.

---

<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:** [April 17, 2006, 12:28am UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/3 "2006-04-17T00:28:48Z")

</div>

I guess the attachment got lost with the forum… do you have a URL?

rusty

Florian GroÃ? wrote:

> Rusty Geldmacher wrote:
> 
> > Recently I’ve had to come up with a way to simulate an enumerated type  
> > for a Rails project that I am working on. I looked around for a bit to  
> > see what is out there and what people are doing now, but nothing I found  
> > would satisfy all of my design requirements. I wrote up a blog entry on  
> > the subject which discusses what our requirements were and how we  
> > addressed them. The results are here:
> > 
> > [http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby](http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-enumeration-pattern-for-ruby)
> 
> Been there, done that. See attachment.

---

<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:** [April 17, 2006, 1:11am UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/4 "2006-04-17T01:11:53Z")

</div>

On Apr 16, 2006, at 2:55 PM, Rusty Geldmacher wrote:

> [http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-](http://www.soapboxsoftware.org/articles/2006/04/15/a-simple-)  
> enumeration-pattern-for-ruby
> 
> I would greatly appreciate feedback from the Ruby community!

Quoted from the article:

"That in itself is not a very challenging or interesting problem. We  
can certainly create a class with a list of constants, such as in the  
example below.

class Direction  
North = 0  
East = 1  
West = 2  
South = 3  
end

Though simple, this solution does not satisfy our design goals. For  
one, it makes it nearly impossible to reverse map values. With that  
class above, how would I determine that the number 2 corresponds to  
the value ?West??"

Reflection:

> > class Direction  
> > North = 0  
> > East = 1  
> > West = 2  
> > South = 3  
> > end  
> > =\> 3  
> > Direction.constants.find { |c| Direction.const\_get(c) == 2 }  
> > =\> “West”

“Another goal it does not satisfy is the ability to iterate through  
the members of the enumeration.”

> > Direction.constants.each { |c| puts “#{c} = #{Direction.const\_get  
> > (c)}” }  
> > South = 3  
> > West = 2  
> > East = 1  
> > North = 0  
> > =\> [“South”, “West”, “East”, “North”]

Both of those are just FYI. I did enjoy reading the article. 🙂

James Edward G. II

---

<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:** [April 17, 2006, 12:38am UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/5 "2006-04-17T00:38:07Z")

</div>

Rusty Geldmacher wrote:

> I guess the attachment got lost with the forum… do you have a URL?

Ah, sounds like an RFE for the [ruby-forum.com](http://ruby-forum.com) software.

I’ve uploaded it to [http://flgr.0x42.net/code/enum.rb](http://flgr.0x42.net/code/enum.rb)

---

<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:** [April 17, 2006, 1:40am UTC](https://www.ruby-forum.com/t/simple-enumerations-in-rub/93968/6 "2006-04-17T01:40:28Z")

</div>

Oh right, I did come across that while poking around for prior work, but  
I thought it could be simpler… I did not like having to instantiate a  
class each time for the enum, and having a seperate class for each  
member. Those are just personal preferences – that one is certainly a  
valid approach! Including Comparable is a good idea, too.

rusty

Florian GroÃ? wrote:

> Rusty Geldmacher wrote:
> 
> > I guess the attachment got lost with the forum… do you have a URL?
> 
> Ah, sounds like an RFE for the [ruby-forum.com](http://ruby-forum.com) software.
> 
> I’ve uploaded it to [http://flgr.0x42.net/code/enum.rb](http://flgr.0x42.net/code/enum.rb)
