From: Xavier Noria Date: 2012-10-24T19:32:21+09:00 Subject: Re: Dynamically creating subclasses --f46d042a0d55ef6a5604cccb9973 Content-Type: text/plain; charset=UTF-8 On Wed, Oct 24, 2012 at 12:25 PM, The Sun wrote: 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. --f46d042a0d55ef6a5604cccb9973 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Oct 24, 2012 at 12:25 PM, The Sun <lists@ruby-forum.com>= wrote:

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.
=C2=A0
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 th= at 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 paren= t's run.
--f46d042a0d55ef6a5604cccb9973--