C/c++ enum equivalent?


#1

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? :slight_smile:

I tried:

module AnEnum
:ValueOne
:ValueTwo

end

But mentioning a constant’s sysmbol doesn’t define it.


#2

On Sat, Mar 18, 2006 at 01:42:51AM +0900, removed_email_address@domain.invalid 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? :slight_smile:
}
} 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


#3

removed_email_address@domain.invalid wrote:

end

But mentioning a constant’s sysmbol doesn’t define it.

That’s true. :slight_smile: 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

#4

On 3/17/06, removed_email_address@domain.invalid removed_email_address@domain.invalid 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.


#5

On 17 Mar 2006, at 16:58, Robert K. 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


#6

On 17 Mar 2006, at 16:49, Gregory S. 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.