# How to do: var1 = var2.new(.....) (var2 contains classname)

**URL:** <https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599>\
**Category:** Ruby\
**Created:** [February 14, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599 "2007-02-14T21:36:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/1 "2007-02-14T21:36:50Z")

</div>

Hi All

I have a situation in which I have the name of the class in a variable  
So normally I would do  
song = Song.new(“Bicylops”, “Fleck”, 260)

But I need to do something like  
var2 = “Song”  
var1 = var2.new(“Bicylops”, “Fleck”, 260)

This doesn’t work!  
Any suggestions how to do this ?

Thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/2 "2007-02-14T21:45:37Z")

</div>

On 2/14/07, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a situation in which I have the name of the class in a variable  
> So normally I would do  
> song = Song.new(“Bicylops”, “Fleck”, 260)
> 
> But I need to do something like  
> var2 = “Song”  
> var1 = var2.new(“Bicylops”, “Fleck”, 260)
> 
> This doesn’t work!  
> Any suggestions how to do this ?

var1 = Object.get\_const(var2).new(“Bicyclops”, “Fleck”, 260)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/3 "2007-02-14T21:46:38Z")

</div>

On 2/14/07, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This doesn’t work!  
> Any suggestions how to do this ?

You need to convert your string “Song” into a Class object.

var2 = “Song”  
var1 = Object.const\_get(var2).new(“Bicyclops”, “Fleck”, 260)

All class and module objects are stored as constants in Object. It  
gets a little tricky if you have a class withing a module –  
“Music::Song”. Then you’ll have to do something like this:

current = Object  
var2 = “Music::Song”  
var2.split(“::”).each do |str|  
current = current.const\_get(str)  
end  
var1 = current.new( … )

Hope this helps

TwP

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/4 "2007-02-14T21:47:38Z")

</div>

Maybe you want to have a look at this:

var1 = Object.const\_get(var2)

Kind regards  
Nicolai

2007/2/14, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/5 "2007-02-14T21:50:17Z")

</div>

Hi

Thanks for the solution!  
It works!!

LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/6 "2007-02-14T21:51:33Z")

</div>

Lyle J. wrote:

> var1 = Object.get\_const(var2).new(“Bicyclops”, “Fleck”, 260)

should be #const\_get()

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 14, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/7 "2007-02-14T21:48:38Z")

</div>

On 2/14/07, Lyle J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> var1 = Object.get\_const(var2).new(“Bicyclops”, “Fleck”, 260)

Whoops, that should be “const\_get(var2)”, not “get\_const(var2)”.

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2007, 9:45am UTC](https://www.ruby-forum.com/t/how-to-do-var1-var2-new-var2-contains-classname/85599/8 "2007-02-15T09:45:37Z")

</div>

Luca S. \<lcalje [gmail.com](http://gmail.com)\> writes:

> Hi All
> 
> I have a situation in which I have the name of the class in a variable  
> So normally I would do  
> song = Song.new(“Bicylops”, “Fleck”, 260)

I’m surprised no one else has mentioned this, but there’s very little  
need to  
store a class name in a string. Usually if you’re doing this then  
there’s  
something wrong with your methodology and there’s a much more efficient  
way to  
get the right results.

The only real reason I can think that you’d have that in a string and  
not simple  
as the class constant is if you took it from user input. Hopefully you  
aren’t  
letting your users decide which classes get instantiated…!
