# Help:dynamic inherit from a class

**URL:** <https://www.ruby-forum.com/t/help-dynamic-inherit-from-a-class/72859>\
**Category:** Ruby\
**Created:** [October 19, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/help-dynamic-inherit-from-a-class/72859 "2006-10-19T13:19:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karl1113](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9bcb4/32.png) [@karl1113](https://www.ruby-forum.com/u/karl1113)\
**Post date:** [October 19, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/help-dynamic-inherit-from-a-class/72859/1 "2006-10-19T13:19:37Z")

</div>

## 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?

---

<div class="post-metadata">

**Author:** ![karl1113](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9bcb4/32.png) [@karl1113](https://www.ruby-forum.com/u/karl1113)\
**Post date:** [October 19, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/help-dynamic-inherit-from-a-class/72859/2 "2006-10-19T13:26:41Z")

</div>

On 10/19/06, Hang L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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.

---

<div class="post-metadata">

**Author:** ![karl1113](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9bcb4/32.png) [@karl1113](https://www.ruby-forum.com/u/karl1113)\
**Post date:** [October 19, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/help-dynamic-inherit-from-a-class/72859/3 "2006-10-19T13:28:48Z")

</div>

Hang L. wrote:

> ```
> 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
```
