[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help:dynamic inherit from a class

Hang Liu

10/19/2006 11:20:00 AM

Hi all:
Now I have problem about dynamiclly inheriting form a class.
For example:
--------
class_name='MyClass'
klass = Object.const_set(class_name, Class.new)
klass.class_eval do
...
end
--------
now i get a class named 'MyClass',then i want the MyClass inherit
from another
class 'AnotherClass',how can I do it?

--
Posted via http://www.ruby-....

2 Answers

chrisjroos@gmail.com

10/19/2006 11:26:00 AM

0

On 10/19/06, Hang Liu <liuhang1113@gmail.com> wrote:
> Hi all:
> Now I have problem about dynamiclly inheriting form a class.
> For example:
> --------
> class_name='MyClass'
> klass = Object.const_set(class_name, Class.new)
> klass.class_eval do
> ...
> end
> --------
> now i get a class named 'MyClass',then i want the MyClass inherit
> from another
> class 'AnotherClass',how can I do it?
>
class MySuperClass; end

Class.new(MySuperClass) #=> create anonymous class that inherits from
MySuperClass.

Vincent Fourmond

10/19/2006 11:28:00 AM

0

Hang Liu wrote:
> Hi all:
> Now I have problem about dynamiclly inheriting form a class.
> For example:
> --------
> class_name='MyClass'
> klass = Object.const_set(class_name, Class.new)
> klass.class_eval do
> ...
> end
> --------
> now i get a class named 'MyClass',then i want the MyClass inherit
> from another
> class 'AnotherClass',how can I do it?

Use the optionnal parameter of Class.new:


------------------------------------------------------------- Class::new
Class.new(super_class=Object) => a_class
------------------------------------------------------------------------
Creates a new anonymous (unnamed) class with the given superclass
(or Object if no parameter is given). You can give a class a name
by assigning the class object to a constant.

Cheers !

Vince