# Dynamic class names

**URL:** <https://rubytalk.org/t/dynamic-class-names/45597>\
**Category:** ruby-talk\
**Created:** [2 April 2008 09:18 UTC](https://rubytalk.org/t/dynamic-class-names/45597 "2008-04-02T09:18:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Boese](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Martin\_Boese](https://rubytalk.org/u/Martin_Boese)\
**Post date:** [2 April 2008 09:18 UTC](https://rubytalk.org/t/dynamic-class-names/45597/1 "2008-04-02T09:18:25Z")

</div>

Is it possible to create classes with a dynamic name? Example:

name = "Test"

class name # this fails  
def write  
&nbsp;&nbsp;&nbsp;&nbsp;puts "TEST"  
&nbsp;&nbsp;end  
end

puts Object::const\_get(name).new.write # =\> "TEST"

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [2 April 2008 09:58 UTC](https://rubytalk.org/t/dynamic-class-names/45597/2 "2008-04-02T09:58:52Z")

</div>

Martin Boese wrote:

> Is it possible to create classes with a dynamic name? Example:
> 
> name = "Test"
> 
> class name # this fails  
> def write  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "TEST"  
> &nbsp;&nbsp;end  
> end
> 
> puts Object::const\_get(name).new.write # =\> "TEST"

Yes. Here is an overview on metaprogramming, and two ways to create  
classes dynamically:

> **[Ruby Metaprogramming techniques](http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html)**
>
> Updated: Scott Labounty wondered how the trace example could work and since a typical metaprogramming technique is writing before- and after...

- -- Phillip Gawlowski

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [2 April 2008 11:19 UTC](https://rubytalk.org/t/dynamic-class-names/45597/3 "2008-04-02T11:19:33Z")

</div>

sure

c = Class::new {  
&nbsp;&nbsp;def a; 41 end  
}  
d = Class::new(c) {  
&nbsp;&nbsp;def a; super.succ end  
}  
d.new.a --\> 42

HTH  
Robert

> **···**
>
> On Wed, Apr 2, 2008 at 11:18 AM, Martin Boese \<boesemar@gmx.de\> wrote:
> 
> > Is it possible to create classes with a dynamic name? Example:
> > 
> > name = "Test"
> > 
> > class name # this fails  
> > &nbsp;&nbsp;def write  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "TEST"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > puts Object::const\_get(name).new.write # =\> "TEST"
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Martin\_Boese](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Martin\_Boese](https://rubytalk.org/u/Martin_Boese)\
**Post date:** [2 April 2008 11:02 UTC](https://rubytalk.org/t/dynamic-class-names/45597/4 "2008-04-02T11:02:44Z")

</div>

No... I mean the 'Name' of a class or module (that what you get with  
object.class.name). The doc of Class::new (ri) describes how it works:

> You can give a class a name by assigning the class object to a constant.

...but still that doesn't allow me to set the initial name when \_defining\_ the  
class.

I am aware that this can be done using eval with the complete class definition  
as a string - which is also not what I want.

The reason is to avoid name clashes of a larger program. So I am thinking of  
using some central instance to assign names and address specific types of  
classes.

Martin

> **···**
>
> On Wednesday 02 April 2008 10:58:52 Phillip Gawlowski wrote:
> 
> > Martin Boese wrote:  
> > \> Is it possible to create classes with a dynamic name? Example:  
> > \>  
> > \> name = "Test"  
> > \>  
> > \> class name # this fails  
> > \> def write  
> > \> puts "TEST"  
> > \> end  
> > \> end  
> > \>  
> > \> puts Object::const\_get(name).new.write # =\> "TEST"
> > 
> > Yes. Here is an overview on metaprogramming, and two ways to create  
> > classes dynamically:
> > 
> > [Ola Bini: Programming Language Synchronicity: Ruby Metaprogramming techniques](http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html)
> > 
> > -- Phillip Gawlowski

---

<div class="post-metadata">

**Author:** ![Martin\_Boese](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Martin\_Boese](https://rubytalk.org/u/Martin_Boese)\
**Post date:** [2 April 2008 12:15 UTC](https://rubytalk.org/t/dynamic-class-names/45597/5 "2008-04-02T12:15:40Z")

</div>

Thanks, that's what I was looking for.

That example of mine now works:

name = 'Test'  
Object::const\_set(name.intern, Class::new do  
def write  
&nbsp;&nbsp;&nbsp;puts "TEST"  
end  
end  
)  
Object::const\_get(name).new.write # writes =\> "TEST"

martin

> **···**
>
> On Wednesday 02 April 2008 12:19:33 Robert Dober wrote:
> 
> > On Wed, Apr 2, 2008 at 11:18 AM, Martin Boese \<boesemar@gmx.de\> wrote:  
> > \> Is it possible to create classes with a dynamic name? Example:  
> > \>  
> > \> name = "Test"  
> > \>  
> > \> class name # this fails  
> > \> def write  
> > \> puts "TEST"  
> > \> end  
> > \> end  
> > \>  
> > \> puts Object::const\_get(name).new.write # =\> "TEST"
> > 
> > sure
> > 
> > c = Class::new {  
> > &nbsp;&nbsp;def a; 41 end  
> > }  
> > d = Class::new(c) {  
> > &nbsp;&nbsp;def a; super.succ end  
> > }  
> > d.new.a --\> 42
> > 
> > HTH  
> > Robert
