On Fri, Jan 27, 2012 at 7:56 AM, Chad Perrin <code / apotheon.net> 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 exampl=
e
>> can be rewritten as:
>>
>> bar =3D "Hello" + "World"
>>
>> klass =3D Class.new do
>> =A0 def initialize(name=3D'World')
>> =A0 =A0 @name =3D name
>> =A0 end
>>
>> =A0 def greet
>> =A0 =A0 return "Hello, #{@name}"
>> =A0 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 =3D []

cl << Class.new do
    def self.my_job?(x)
      x % 2 =3D=3D 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 !=3D 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 =3D cl.find {|c| c.my_job? i}
irb(main):024:1>
irb(main):025:1*   if cls
irb(main):026:2>     obj =3D 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.
=3D> 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/