Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help:dynamic inherit from a class

Reply
Thread Tools

Help:dynamic inherit from a class

 
 
Hang Liu
Guest
Posts: n/a
 
      10-19-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Chris Roos
Guest
Posts: n/a
 
      10-19-2006
On 10/19/06, Hang Liu <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Vincent Fourmond
Guest
Posts: n/a
 
      10-19-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want - but cannot get - a nested class to inherit from outer class DBak Python 11 03-08-2008 01:38 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
inherit from class which inherits from Page class djus@poczta.onet.pl ASP .Net 0 07-14-2004 12:17 PM
Accessing session data in a class file- which class do I inherit from? Kevin Spencer ASP .Net 6 04-22-2004 11:32 PM
VStudio and ASP.NET problem/limitation when pages inherit from an Abstract class? DesignerX ASP .Net 0 08-07-2003 09:35 PM



Advertisments