From: Mathieu Bouchard Date: 2001-11-04T01:20:22+09:00 Subject: [ruby-talk:24271] Re: C-like enum On Fri, 2 Nov 2001, Jason Voegele wrote: > Here's an improved version based on input from David Alan Black and Nat Pryce: > module Kernel > # simple (sequential) enumerated values > def enum(*syms) > n = block_given? ? yield : 0 > syms.each_with_index { |s,i| const_set(s,i+n) } > end > # enumerated values suitable for bitwise OR-ing > def set(*syms) > syms.each_with_index { |s,i| const_set(s,2**i) } > end > end I have something similar called "ChoiceType" in RubyX11 and MetaRuby. It's somewhat different because objects in class ChoiceType know which elements are in their set, no constants are created; you'd typically do something like: CardColor = ChoiceType.new(:hearts,:diamonds,:clubs,:spades) where all four symbols are considered values of that type, and although values 0..3 are associated with these elements, those integers only show up when you query the type about it: otherwise you use the symbols themselves. in RubyX11 and MetaRuby, a "Type" is something more general than a "Class". a "Type" is little more than a "set", and a "ChoiceType" is a type where all the values are listed in advance (contrary to other types). ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju