From: "Ara.T.Howard" Date: 2005-08-03T05:39:00+09:00 Subject: Re: enum collection of constant values On Wed, 3 Aug 2005, Joel VanderWerf wrote: > Ara.T.Howard wrote: >> class Object >> def enum(*list) >> mc = Module === self ? self : self.class >> list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, i} >> end >> def bit_enum(*list) >> mc = Module === self ? self : self.class >> list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, 2 >> ** i} >> end >> end > > Why would you want the mc = self.class option ? > > irb(main):022:0> x=[] > => [] > irb(main):023:0> x.instance_eval { enum %w{ZZZ AAA} } > => ["ZZZ", "AAA"] > irb(main):024:0> ZZZ > NameError: uninitialized constant ZZZ > from (irb):24 > irb(main):025:0> Array::ZZZ > => 0 isn't this what the op wanted? harp:~ > cat a.rb def try begin yield rescue => e STDERR.puts "#{ e.message } (#{ e.class })" end end class Object def enum(*list) mc = self list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, i} end def bit_enum(*list) mc = self list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, 2 ** i} end end try do enum %w( FOO BAR ) p FOO end class Object def enum(*list) mc = Module === self ? self : self.class list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, i} end def bit_enum(*list) mc = Module === self ? self : self.class list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, 2 ** i} end end try do enum %w( FOO BAR ) p FOO end harp:~ > ruby a.rb undefined method `const_set' for main:Object (NoMethodError) 0 the 'mc = self.class' is when 'self == main'. maybe i misunderstood? regards. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================