# Creating objects type only known at runtime

**URL:** <https://www.ruby-forum.com/t/creating-objects-type-only-known-at-runtime/73278>\
**Category:** Ruby\
**Created:** [October 23, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/creating-objects-type-only-known-at-runtime/73278 "2006-10-23T14:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [October 23, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/creating-objects-type-only-known-at-runtime/73278/1 "2006-10-23T14:45:13Z")

</div>

This weekend I was playing around with some ruby and wanted  
to create objects whose types were only known at runtime.  
The method I wound up using was

o = eval “#{class\_name}.new”

Is this a preferred method. I come from (mostly) a perl  
background, but also a Java background. To me this approach  
seems inelegant.

Thanks for any input.

Andy

---

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [October 23, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/creating-objects-type-only-known-at-runtime/73278/2 "2006-10-23T14:49:13Z")

</div>

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

> 

o = Object.const\_get(class\_name).new

Farrel

---

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [October 23, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/creating-objects-type-only-known-at-runtime/73278/3 "2006-10-23T16:16:57Z")

</div>

Farrel L. wrote:

> > seems inelegant.
> 
> o = Object.const\_get(class\_name).new
> 
> Farrel

Thanks!!
