Forum: Ruby c/c++ enum equivalent?

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.
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 unknown (Guest)
on 2006-03-17 17:43
(Received via mailing list)
I'd like to fill a module with valueless constants (because I don't care
about the actual values, just that they are uneque, and named) - is that
a good thing to do, and can I do that? :)

I tried:

module AnEnum
  :ValueOne
  :ValueTwo
  ...
end

But mentioning a constant's sysmbol doesn't define it.
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-03-17 17:52
(Received via mailing list)
On Sat, Mar 18, 2006 at 01:42:51AM +0900, benjohn@fysh.org wrote:
} I'd like to fill a module with valueless constants (because I don't
care
} about the actual values, just that they are uneque, and named) - is
that
} a good thing to do, and can I do that? :)
}
} I tried:
}
} module AnEnum
}   :ValueOne
}   :ValueTwo
}   ...
} end
}
} But mentioning a constant's sysmbol doesn't define it.

Those are symbols, not constants. And they are what you want. You don't
need to define them anywhere. They are defined by being used.

--Greg
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-17 18:02
(Received via mailing list)
benjohn@fysh.org wrote:
> end
>
> But mentioning a constant's sysmbol doesn't define it.

That's true. :-)  If you just need the names you can just use symbols
(you don't even have to define them).  You could do

module AnEnum
   VALUES = [
     :foo,
     :bar,
     :dummy,
   ].freeze
end

If you look for more sophisticated solutions: There was a posting about
this around 9. and 10. March this year with subject "Enums (was: My Ruby
talk @ work..)".  You'll also likely find something in the RAA:

http://raa.ruby-lang.org/

Kind regards

	robert
C5be24289f1471f3da84864a6677af12?d=identicon&s=25 Garance A Drosehn (Guest)
on 2006-03-19 01:35
(Received via mailing list)
On 3/17/06, benjohn@fysh.org <benjohn@fysh.org> wrote:
>
> I'd like to fill a module with valueless constants (because I don't care
> about the actual values, just that they are unique, and named)

# -------+---------+---------+-------- +
--------+---------+---------+---------+
#   Add methods enum and bit_enum to the Object class, thus making it
much
#   easier and less error-prone to define a long list of constants in a
#   class.  This idea comes from Ara.T.Howard@noaa, in a posting to
ruby-talk
#   on August 2, 2005 (in reply to a question).  These work very nicely
with
#   a word-list array as generated via %w().  Very clever!
#
class Object
    def enum_constants(*list)
    mc = Module === self ? self : self.class
    list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, i}
    end
    def enum_bit_constants(*list)
    mc = Module === self ? self : self.class
    list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, 2 **
i}
    end
end

# -------+---------+---------+-------- +
--------+---------+---------+---------+

An example of use:

    #   Constants that define all categories of packets which we
recognize.
    enum_constants %w(
	  SBC_NONE       SBC_ARRAY    SBC_GOODPW   SBC_GOODKEY  SBC_GOODNONE
	  SBC_BADPW      SBC_BADKEY   SBC_BADNONE  SBC_IGNFAIL  SBC_ALL
	  SBC_UNMATCHED
    )				#  "UNMATCHED" should be the last one.
    SBC_ENUMCOUNT = SBC_UNMATCHED + 1;

I've been meaning to add a few more bells and whistles to this,
such as having a way for 'enum_constants' to automatically
handle the definition of values such as SBC_ENUMCOUNT.
But of course I have a few million things that "I've been meaning
to do", and never get around to.

Someone else had a module of some sort for enums.  I forget
who, but it was someone on this mailing list in the last month
or so.  Their version was very different from the above, and it
in some situations what they did would have some significant
advantages over the simple solution I like to use.
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-03-20 22:45
(Received via mailing list)
On 17 Mar 2006, at 16:49, Gregory Seidman wrote:

> }   :ValueOne
> }   :ValueTwo
> }   ...
> } end
> }
> } But mentioning a constant's sysmbol doesn't define it.
>
> Those are symbols, not constants. And they are what you want. You
> don't
> need to define them anywhere. They are defined by being used.

Hi Greg, thank you ... I suppose what I wanted was to be able to write:
	AnEnum::ValueIDidntDefine

... in some code, and have Ruby come back and bash me for talking
about something that's not in the set of allowed values.
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-03-20 22:47
(Received via mailing list)
On 17 Mar 2006, at 16:58, Robert Klemme wrote:

>>   ...
>     :dummy,
>   ].freeze
> end
>
> If you look for more sophisticated solutions: There was a posting
> about this around 9. and 10. March this year with subject "Enums
> (was: My Ruby talk @ work..)".

Thank you, I will take a look for that thread.

*snips*
This topic is locked and can not be replied to.