From: Hugh Sasse Staff Elec Eng Date: 2000-03-16T17:16:06+00:00 Subject: [ruby-talk:01905] Re: Enumerations and all that. On 16 Mar 2000, Dave Thomas wrote: > Hugh Sasse Staff Elec Eng writes: > > > Thank you to the people who responded to my questions about > > Enumerated types. I have put something together that does what I > > want now, and have some code to (not rigorously) test it. It may > > have major weaknesses that I have not seen, so I would welcome > > comments on it. > > I was wondering: if, rather than making each enumeration an instance, > you were instead to define a new class for it (probably itself a > subclass of Enumeration), then there'd be no need for the rest of the > user-level code to keep an object reference lying around. You could > say something like I am not sure how to do that on the fly. In what binding can I eval("class #{enum_nam}...end",???) to create the new class? > > Enumeration.create("Colours", { RED => 1, GREEN => 2, ... }) then I need another instance of class (Pascal would call it a VAR in contrast to my enum TYPE) which can hold values only available in the enum type. That latter type would alow operations on it rather than declaration. I thought it simpler to keep them together, and use the @_value to access the VAR. > > and then elsewhere in the code simply reference > > Colours::RED > Colours::GREEN > > and so on. > > If you wanted to go that route, the method Module#const_set would be > useful. I don't really understand this. Each instance needs to be tied to some enumeraton class, so it only holds values allowed in that enum, but if I have a Module then I cannot have instances of a Module, because the FAQ says that is the difference between Module and Class. If it is to have a readonly thing within each class that is stored in one place, I wondered if I caould do that by making @_fields into Enumeration.fields, so it is a class variable, but I could not get that to work. I am only sure that class methods and singleons can be make to exist, not class variables shared across all instances. > > Regards > > > Dave > Hugh hgs@dmu.ac.uk