Forum: Ruby Simple Enumerations in Rub

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
801783653bbf14efccd51a3122ca0ea8?d=identicon&s=25 Rusty Geldmacher (Guest)
on 2006-04-16 21:55
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...

I would greatly appreciate feedback from the Ruby community!

Thanks,
rusty
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-04-16 23:21
(Received via mailing list)
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...

Been there, done that. See attachment.
801783653bbf14efccd51a3122ca0ea8?d=identicon&s=25 Rusty Geldmacher (geldmacher)
on 2006-04-17 00:28
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...
>
> Been there, done that. See attachment.
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-04-17 00:38
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-17 01:11
(Received via mailing list)
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(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 Gray II
801783653bbf14efccd51a3122ca0ea8?d=identicon&s=25 Rusty Geldmacher (geldmacher)
on 2006-04-17 01:40
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
This topic is locked and can not be replied to.