From: Robert Klemme Date: 2012-01-30T07:28:40+09:00 Subject: Re: A little assistance please :) On Fri, Jan 27, 2012 at 7:56 AM, Chad Perrin wrote: > On Fri, Jan 27, 2012 at 12:20:08PM +0900, Josh Cheek wrote: >> >> It's incredibly rare to need to resort to string evaluation. This example >> can be rewritten as: >> >> bar = "Hello" + "World" >> >> klass = Class.new do >>   def initialize(name='World') >>     @name = name >>   end >> >>   def greet >>     return "Hello, #{@name}" >>   end >> end >> >> Object.const_set bar, klass > > You're right, that's much better -- apart from the fact I have yet to > come up with a single good reason for dynamic class names. If the class name is dynamic (meaning, it comes from some input outside the program code) then the only reasonable thing is to derive the definition of the class also from input. Having a class with a dynamic name whose behavior is always defined in the same way would be useless. Also, you would want to have several of them because otherwise a single class with a fixed name and input driven definition would be sufficient. If you have multiple classes with dynamic definitions (and probably names as well, but they could also be unnamed) then you need a way to select them. This could be something which is used as key in a Hash (quite efficient) - but you could also have a dynamic mechanism, where each class is asked whether it feels responsible. cl = [] cl << Class.new do def self.my_job?(x) x % 2 == 0 end def show(x) printf "I EVEN show you %d.\n", x end end cl << Class.new do def self.my_job?(x) x % 2 != 0 end def show(x) printf "%d is ODD.\n", x end end irb(main):022:0> 5.times do |i| irb(main):023:1* cls = cl.find {|c| c.my_job? i} irb(main):024:1> irb(main):025:1* if cls irb(main):026:2> obj = cls.new irb(main):027:2> obj.show i irb(main):028:2> else irb(main):029:2* $stderr.printf "Nobody wants %d\n", i irb(main):030:2> end irb(main):031:1> end I EVEN show you 0. 1 is ODD. I EVEN show you 2. 3 is ODD. I EVEN show you 4. => 5 But all this does not seem the case for the OP's piece of code, so we're rather abducting the thread. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/