Garance A Drosehn
2/5/2007 11:34:00 PM
On 2/3/07, Dan Stevens (IAmAI) <dan.stevens.iamai@gmail.com> wrote:
> As part of a solution, I wish the value of an attribute to be one of a
> finite set of values, in other words I believe I want an 'enumerated
> type'. I've look at the 'Enumerable' and 'Enumerator' class and they
> appear to me to be unrelated to what I wish to achieve (correct me if
> I'm wrong).
>
> I could do the traditional method of named constants with integer
> values but I can't help feeling that there must be a class or some
> nice method of doing this. Any advice would be appreciated.
I picked up this snippet of code:
# -------+---------+---------+-------- + --------+---------+---------+---------+
# 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 using it:
# Constants that define all categories of packets which we recognize.
enum_constants %w(
SBC_NONE SBC_ARRAY SBC_NONUSER SBC_NO_IDSTR
SBC_IGNFAIL SBC_ALL
SBC_UNMATCHED )
The difference between enum_constants() and enum_bit_constants is
that enum_constants will define the values as 1, 2, 3, 4, 5, etc, while
enum_bit_constants will define them as 1,2,4,8,16,32. You can 'or'
the enum_bit_constant values together, and each one is a separate
bit so it won't clobber any other bit constant.
If you are not used to the %w() method of quoting, note that you only
put the names you want in there. You do *not* separate them with
commas!
--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA