# Dynamic object creation

**URL:** <https://rubytalk.org/t/dynamic-object-creation/6634>\
**Category:** ruby-talk\
**Created:** [30 June 2003 16:01 UTC](https://rubytalk.org/t/dynamic-object-creation/6634 "2003-06-30T16:01:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryeh\_Friedman](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Aryeh\_Friedman](https://rubytalk.org/u/Aryeh_Friedman)\
**Post date:** [30 June 2003 16:01 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/1 "2003-06-30T16:01:25Z")

</div>

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a  
string how do I create an instance of the class the string contains?

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [30 June 2003 16:02 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/2 "2003-06-30T16:02:00Z")

</div>

How about this ?

s=“Array”  
aObject=eval(s+“.new”)

> **···**
>
> On Mon, 30 Jun 2003 16:49:05 +0000, Aryeh Friedman wrote:
> 
> > If I have something like this:
> > 
> > s=“Array”
> > 
> > How whould I get something like this:
> > 
> > aObject=s.new
> > 
> > In “plain” english I am asking if I have a valid name of a class in a  
> > string how do I create an instance of the class the string contains?
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [30 June 2003 16:14 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/3 "2003-06-30T16:14:24Z")

</div>

A class name is just a constant, so something like

a = Module.const\_get(“Array”).new

is what you need (Aside: what’s the proper way to access ‘the top-level  
Module’, or is the above correct?)

Cheers,

Brian.

> **···**
>
> On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:
> 
> > If I have something like this:
> > 
> > s=“Array”
> > 
> > How whould I get something like this:
> > 
> > aObject=s.new
> > 
> > In “plain” english I am asking if I have a valid name of a class in a  
> > string how do I create an instance of the class the string contains?

---

<div class="post-metadata">

**Author:** ![stefano](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@stefano](https://rubytalk.org/u/stefano)\
**Post date:** [30 June 2003 16:22 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/4 "2003-06-30T16:22:22Z")

</div>

s = “Array”  
aObject = eval(s).new

> **···**
>
> On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:
> 
> > If I have something like this:
> > 
> > s=“Array”
> > 
> > How whould I get something like this:
> > 
> > aObject=s.new
> > 
> > In “plain” english I am asking if I have a valid name of a class in a  
> > string how do I create an instance of the class the string contains?
> 
> –  
> ste

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [30 June 2003 20:01 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/5 "2003-06-30T20:01:08Z")

</div>

… and just hope nobody passes in s = “`rm -rf /*`” as a class name 😑

const\_get will be much faster too, as it doesn’t have to compile a piece of  
code each time.

> **···**
>
> On Tue, Jul 01, 2003 at 01:22:22AM +0900, stefano wrote:
> 
> > On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:
> > 
> > > If I have something like this:
> > > 
> > > s=“Array”
> > > 
> > > How whould I get something like this:
> > > 
> > > aObject=s.new
> > > 
> > > In “plain” english I am asking if I have a valid name of a class in a  
> > > string how do I create an instance of the class the string contains?
> > 
> > s = “Array”  
> > aObject = eval(s).new

---

<div class="post-metadata">

**Author:** ![Anders\_Borch2](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anders\_Borch2](https://rubytalk.org/u/Anders_Borch2)\
**Post date:** [30 June 2003 20:23 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/6 "2003-06-30T20:23:35Z")

</div>

Brian Candler wrote:

> > If I have something like this:
> > 
> > s=“Array”
> > 
> > How whould I get something like this:
> > 
> > aObject=s.new
> > 
> > In “plain” english I am asking if I have a valid name of a class in a  
> > string how do I create an instance of the class the string contains?
> 
> A class name is just a constant, so something like
> 
> a = Module.const\_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

[http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)

says that I need to supply a symbol, why does it accept a string?

is this something that will continue to work in 1.8? (im still using  
1.6.8 on my winbox and 1.6.7 on my linux box)

> **···**
>
> > On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:
> 
> > is what you need (Aside: what’s the proper way to access ‘the top-level  
> > Module’, or is the above correct?)
> > 
> > Cheers,
> > 
> > Brian.
> 
> –  
> dc -e  
> 4dd_od3_dddn1-89danrn10-dan3+ann6_dan_2_an13dn1+dn2-dn3+5_ddan2/9+an13nap

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [2 July 2003 11:37 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/7 "2003-07-02T11:37:21Z")

</div>

“Brian Candler” [B.Candler@pobox.com](mailto:B.Candler@pobox.com) schrieb im Newsbeitrag  
[news:20030630171414.A19372@linnet.org](mailto:news:20030630171414.A19372@linnet.org)…

> **···**
>
> > On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:
> > 
> > > If I have something like this:
> > > 
> > > s=“Array”
> > > 
> > > How whould I get something like this:
> > > 
> > > aObject=s.new
> > > 
> > > In “plain” english I am asking if I have a valid name of a class in a  
> > > string how do I create an instance of the class the string contains?
> > 
> > A class name is just a constant, so something like
> > 
> > a = Module.const\_get(“Array”).new
> > 
> > is what you need (Aside: what’s the proper way to access ‘the top-level  
> > Module’, or is the above correct?)
> 
> I think this does it
> 
> a = Kernel.const\_get(“Array”).new
> 
> ```
> robert
> 
> ```

---

<div class="post-metadata">

**Author:** ![John\_Platte](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@John\_Platte](https://rubytalk.org/u/John_Platte)\
**Post date:** [30 June 2003 20:34 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/8 "2003-06-30T20:34:15Z")

</div>

On the page you referenced, the entry for const\_defined? says it takes  
a symbol too, but the example code uses a string argument…!? Same for  
const\_set.

> **···**
>
> On Monday, Jun 30, 2003, at 15:23 America/Chicago, Anders Borch wrote:
> 
> > > A class name is just a constant, so something like  
> > > a = Module.const\_get(“Array”).new
> > 
> > uhm… why does this work?
> > 
> > I just tested it and to my surprise it worked?!
> > 
> > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > 
> > says that I need to supply a symbol, why does it accept a string?
> > 
> > is this something that will continue to work in 1.8? (im still using  
> > 1.6.8 on my winbox and 1.6.7 on my linux box)
> 
> –  
> John Platte  
> Principal Consultant, NIKA Consulting  
> [http://nikaconsulting.com/](http://nikaconsulting.com/)  
> (630) 499 9830  
> Fax: (630) 566 0655

---

<div class="post-metadata">

**Author:** ![stefano](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@stefano](https://rubytalk.org/u/stefano)\
**Post date:** [1 July 2003 08:07 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/9 "2003-07-01T08:07:26Z")

</div>

> … and just hope nobody passes in s = “`rm -rf /*`” as a class name 😑

obviously, if you don’t check what you’re passing to eval(), you deserve  
all the bad things that could happen 🙂

> const\_get will be much faster too, as it doesn’t have to compile a piece of  
> code each time.

that’s true, I didn’t think about it.

> **···**
>
> On Tue, 01 Jul 2003 05:01:08 +0900, Brian Candler wrote:
> 
> –  
> ste

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 July 2003 09:20 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/10 "2003-07-01T09:20:27Z")

</div>

Even if it didn’t, you can easily convert strings to symbols and vice versa.

```
"Array".intern
 => :Array

:Array.to_s
 => "Array"

```

Regards,

Brian.

> **···**
>
> On Tue, Jul 01, 2003 at 05:23:35AM +0900, Anders Borch wrote:
> 
> > > A class name is just a constant, so something like
> > > 
> > > a = Module.const\_get(“Array”).new
> > 
> > uhm… why does this work?
> > 
> > I just tested it and to my surprise it worked?!
> > 
> > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > 
> > says that I need to supply a symbol, why does it accept a string?

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [2 July 2003 11:43 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/11 "2003-07-02T11:43:16Z")

</div>

> I think this does it

> a = Kernel.const\_get("Array").new

svg% ruby -e 'Kernel::Array; Object::Array'  
-e:1: warning: toplevel constant Array referenced by Kernel::Array  
svg%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)\
**Post date:** [30 June 2003 20:37 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/12 "2003-06-30T20:37:19Z")

</div>

John Platte wrote:

> On the page you referenced, the entry for const\_defined? says it takes a  
> symbol too, but the example code uses a string argument…!? Same for  
> const\_set.

> > I just tested it and to my surprise it worked?!
> > 
> > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > 
> > says that I need to supply a symbol, why does it accept a string?

At the top of the section, you’ll find “In the descriptions that follow,  
the parameter aSymbol refers to a symbol, which is either a quoted  
string or a Symbol (such as :name).”

Cheers

Dave

---

<div class="post-metadata">

**Author:** ![John\_Platte](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@John\_Platte](https://rubytalk.org/u/John_Platte)\
**Post date:** [30 June 2003 20:38 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/13 "2003-06-30T20:38:41Z")

</div>

[http://rubycentral.com/book/ref\_c\_module.htm](http://rubycentral.com/book/ref_c_module.htm)

I spoke too soon. At the top of the page:

> In the descriptions that follow, the parameter aSymbol refers to a  
> symbol, which is either a quoted string or a Symbol (such as :name )

Sorry for the spurious post.

> **···**
>
> On Monday, Jun 30, 2003, at 15:33 America/Chicago, John Platte wrote:
> 
> > On the page you referenced, the entry for const\_defined? says it takes  
> > a symbol too, but the example code uses a string argument…!? Same  
> > for const\_set.
> > 
> > On Monday, Jun 30, 2003, at 15:23 America/Chicago, Anders Borch wrote:
> > 
> > > > A class name is just a constant, so something like  
> > > > a = Module.const\_get(“Array”).new
> > > 
> > > uhm… why does this work?
> > > 
> > > I just tested it and to my surprise it worked?!
> > > 
> > > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > > 
> > > says that I need to supply a symbol, why does it accept a string?
> > > 
> > > is this something that will continue to work in 1.8? (im still using  
> > > 1.6.8 on my winbox and 1.6.7 on my linux box)
> 
> –  
> John Platte  
> Principal Consultant, NIKA Consulting  
> [http://nikaconsulting.com/](http://nikaconsulting.com/)  
> (630) 499 9830  
> Fax: (630) 566 0655

---

<div class="post-metadata">

**Author:** ![Anders\_Borch2](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anders\_Borch2](https://rubytalk.org/u/Anders_Borch2)\
**Post date:** [1 July 2003 10:28 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/14 "2003-07-01T10:28:21Z")

</div>

Brian Candler wrote:

> > > A class name is just a constant, so something like
> > > 
> > > a = Module.const\_get(“Array”).new
> > 
> > uhm… why does this work?
> > 
> > I just tested it and to my surprise it worked?!
> > 
> > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > 
> > says that I need to supply a symbol, why does it accept a string?
> 
> Even if it didn’t, you can easily convert strings to symbols and vice versa.
> 
> ```
> "Array".intern
> => :Array
> 
> ```

hadn’t seen that one, thanks =)

> **···**
>
> > On Tue, Jul 01, 2003 at 05:23:35AM +0900, Anders Borch wrote:
> 
> > ```
> > :Array.to_s
> > => "Array"
> > 
> > ```
> > 
> > Regards,
> > 
> > Brian.
> 
> –  
> dc -e  
> 4dd_od3_dddn1-89danrn10-dan3+ann6_dan_2_an13dn1+dn2-dn3+5_ddan2/9+an13nap

---

<div class="post-metadata">

**Author:** ![Anders\_Borch2](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anders\_Borch2](https://rubytalk.org/u/Anders_Borch2)\
**Post date:** [30 June 2003 21:03 UTC](https://rubytalk.org/t/dynamic-object-creation/6634/15 "2003-06-30T21:03:55Z")

</div>

Dave Thomas wrote:

> John Platte wrote:
> 
> > On the page you referenced, the entry for const\_defined? says it takes  
> > a symbol too, but the example code uses a string argument…!? Same  
> > for const\_set.
> 
> > > I just tested it and to my surprise it worked?!
> > > 
> > > [http://rubycentral.com/book/ref\_c\_module.html#Module.const\_get](http://rubycentral.com/book/ref_c_module.html#Module.const_get)
> > > 
> > > says that I need to supply a symbol, why does it accept a string?
> 
> At the top of the section, you’ll find “In the descriptions that follow,  
> the parameter aSymbol refers to a symbol, which is either a quoted  
> string or a Symbol (such as :name).”

thanks alot 🙂 I’ll try to _read_ from now on 😉

> **···**
>
> –  
> dc -e  
> 4dd_od3_dddn1-89danrn10-dan3+ann6_dan_2_an13dn1+dn2-dn3+5_ddan2/9+an13nap
