From: Logan Capaldo Date: 2007-02-06T05:06:49+09:00 Subject: Re: Enumerated Types ------=_Part_64095_6776233.1170706005788 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/5/07, Dan Stevens (IAmAI) wrote: > > 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? This is fine. You may want to use a hash to make life simpler though: class Color HexValues = { :red => 0xFF0000, :green => 0x00FF00, :blue => 0x0000FF } def to_hex HexValues[@color] or @color end end 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 > > > > > > ------=_Part_64095_6776233.1170706005788--