You can use an array or hash for this purpose (in your .rb environment):
OPTIONS = ['one', 'two', 'three'] OPTIONS = {:one => 1, :two => 2, :three => 3}
or, alternatively, an enumeration class that allows you to list your constants, as well as the keys used to bind them:
class Enumeration def Enumeration.add_item(key,value) @hash ||= {} @hash[key]=value end def Enumeration.const_missing(key) @hash[key] end def Enumeration.each @hash.each {|key,value| yield(key,value)} end def Enumeration.values @hash.values || [] end def Enumeration.keys @hash.keys || [] end def Enumeration.[](key) @hash[key] end end
from which you can get:
class Values < Enumeration self.add_item(:RED, '#f00') self.add_item(:GREEN, '#0f0') self.add_item(:BLUE, '#00f') end
and use like this:
Values::RED => '#f00' Values::GREEN => '#0f0' Values::BLUE => '#00f' Values.keys => [:RED, :GREEN, :BLUE] Values.values => ['#f00', '#0f0', '#00f']
Codebeef Nov 05 '08 at 16:41 2008-11-05 16:41
source share