Help:dynamic inherit from a class

Discussion in 'Ruby' started by Hang Liu, Oct 19, 2006.

  1. Hang Liu

    Hang Liu Guest

    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/.
    Hang Liu, Oct 19, 2006
    #1
    1. Advertising

  2. Hang Liu

    Chris Roos Guest

    On 10/19/06, 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?
    >

    class MySuperClass; end

    Class.new(MySuperClass) #=> create anonymous class that inherits from
    MySuperClass.
    Chris Roos, Oct 19, 2006
    #2
    1. Advertising

  3. 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
    Vincent Fourmond, Oct 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DesignerX
    Replies:
    0
    Views:
    353
    DesignerX
    Aug 7, 2003
  2. =?Utf-8?B?Qmx1ZWZsb3dlcg==?=

    Casting DataTable to class inherit from DataTable

    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    994
    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=
    Apr 2, 2004
  3. Kevin Spencer
    Replies:
    6
    Views:
    6,240
    Charlie@CBFC
    Apr 23, 2004
  4. Replies:
    0
    Views:
    364
  5. DBak
    Replies:
    11
    Views:
    1,002
Loading...

Share This Page