Simple Enumerations in Rub


#1

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

I would greatly appreciate feedback from the Ruby community!

Thanks,
rusty


#2

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

Been there, done that. See attachment.


#3

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

Been there, done that. See attachment.


#4

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

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© == 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
©}” }
South = 3
West = 2
East = 1
North = 0
=> [“South”, “West”, “East”, “North”]

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

James Edward G. II


#5

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 software.

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


#6

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 software.

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