From: "Dan Stevens (IAmAI)" Date: 2007-02-06T04:54:11+09:00 Subject: Re: Enumerated Types It seems to me that I should simply be able to use symbols as values for my enumerated types, like for example: class Color def initialize(color) @color = color end def to_hex case @color when :red: 0xFF0000 when :green: 0x00FF00 when :blue: 0x0000FF else @color end end end c = Color.new(:red) puts c.to_hex.to_s(16) Might there be any reason why I shouldn't do this, or am I quite fine doing this? On 03/02/07, Robert Klemme wrote: > On 03.02.2007 19:52, Dan Stevens (IAmAI) 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 believe there are enum classes in the RAA. If you do not want to use > them, symbols come pretty close: > > class YourClass > YOUR_ENUM = [ > :old, > :young, > :unknown, > ].freeze > end > > HTH > > Kind regards > > robert > >