I have to dynamically create a number of classes with different names.
All of them have to inherit from the same subclass.
I tried the following:
eval(class myName < ParentClass; end) where myName is a String
dynamically set together
or the same without eval.
In fact i succeeded in creating objects of the new classes (myName.new)
and the name seems to be set right, but the instantiation does not call
the parents constructor.
1.9.3p194 :001 > class A; def initialize; puts "new A"; end; end
=> nil
1.9.3p194 :003 > name = "X"
=> "X"
1.9.3p194 :007 > Object.const_set name, Class.new(A)
=> X
1.9.3p194 :008 > X
=> X
1.9.3p194 :009 > X.new
new A
=> #<X:0x007fad242a1448>
1.9.3p194 :010 >
···
On Wednesday, 24 October 2012 at 9:25 PM, The Sun wrote:
Hi,
I have to dynamically create a number of classes with different names.
All of them have to inherit from the same subclass.
I tried the following:
eval(class myName < ParentClass; end) where myName is a String
dynamically set together
or the same without eval.
In fact i succeeded in creating objects of the new classes (myName.new)
and the name seems to be set right, but the instantiation does not call
the parents constructor.
I have to dynamically create a number of classes with different names.
All of them have to inherit from the same subclass.
I tried the following:
eval(class myName < ParentClass; end) where myName is a String
Guess that's a typo, make sure the string is a constant name, the class
keyword does not accept identifiers that start in lowercase.
dynamically set together
or the same without eval.
The way to do this using APIs would go through Class.new and const_set, but
I believe that eval approach is just fine and more obvious to the reader of
the code.
In fact i succeeded in creating objects of the new classes (myName.new)
and the name seems to be set right, but the instantiation does not call
the parents constructor.
If MyName has an initialize method, it must call super by hand somewhere to
get the parent's run.
···
On Wed, Oct 24, 2012 at 12:25 PM, The Sun <lists@ruby-forum.com> wrote:
Please don't. It's no use to assign the name to a constant because if
you do not know names beforehand (i.e. when writing the program) as
seems to be the case here you need some form of lookup anyway. You
could use eval again, but a more simpler and robust approach (because
it avoids name clashes) is this:
dyn_classes = {}
name = "DynamicName1"
dyn_classes[name] = Class.new(ParentClass)
Btw, these classes are fairly useless because the only distinguishing
factor is the name. You'd also have to generate custom methods for
each of those. What is the purpose of these generated classes?
Cheers
robert
···
On Wed, Oct 24, 2012 at 2:52 PM, The Sun <lists@ruby-forum.com> wrote: