From: Miles Georgi Date: 2008-09-06T10:12:51+09:00 Subject: Re: dynamically assigning class constants there might be a method for specifically creating constants,but if not you could always do something like this: module_eval("#{name} = #{i*1000}", __FILE__, __LINE__) On Fri, Sep 5, 2008 at 4:44 PM, Pete Hodgson wrote: > Apologies, s/Comparable/Level/ in the example code. > > Pete Hodgson wrote: >> >> Hi folks, >> >> I have a class definition as follows: >> >> class Level >> attr_reader :name, :rank >> def initialize( name, rank ) >> @name, @rank = name, rank >> end >> >> TRACE = Comparable.new( :TRACE, 1000 ) >> DEBUG = Comparable.new( :DEBUG, 2000 ) >> INFO = Comparable.new( :INFO, 3000 ) >> # etc >> end >> >> and I'd like to define the constants in a more ruby-esque way; something >> like: >> >> class Level >> attr_reader :name, :rank >> def initialize( name, rank ) >> @name, @rank = name, rank >> end >> >> %w{TRACE DEBUG INFO CHANGE WARN ERROR FATAL}.each_with_index do >> |name,i| >> clever_dynamic_hackery( name, Comparable.new( name.to_sym, i*1000 ) ) >> end >> >> end >> >> >> but I don't know what that clever_dyanamic_hackery method should >> actually be. Any one want to show me the light? >> >> Cheers, >> Pete >> >> > >