# creating and naming variables dynamic

**URL:** <https://discuss.rubyonrails.org/t/creating-and-naming-variables-dynamic/28665>\
**Category:** rubyonrails-talk\
**Created:** [April 28, 2008, 3:05pm UTC](https://discuss.rubyonrails.org/t/creating-and-naming-variables-dynamic/28665 "2008-04-28T15:05:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher1](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@Tarscher1](https://discuss.rubyonrails.org/u/Tarscher1)\
**Post date:** [April 28, 2008, 3:05pm UTC](https://discuss.rubyonrails.org/t/creating-and-naming-variables-dynamic/28665/1 "2008-04-28T15:05:42Z")

</div>

Hi all,

I want to create a variable dynamic. I have defined some classes and depending on data I get from a database I need to construct an object eg:

now I do

if type=="car" &nbsp;&nbsp;variab = Car.new elsif type="boat" &nbsp;&nbsp;variab = Boat.new end

can I do something like variab = Instance.new(type)

Following this: can I give dynamic variablenames? now I do if type=="car" &nbsp;&nbsp;car\_string = Car.new elsif type="boat" &nbsp;&nbsp;boat\_string = Boat.new end

can I do something like dynamicpart\_string = Instance.new(type)

Regards, Stijn

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 28, 2008, 3:14pm UTC](https://discuss.rubyonrails.org/t/creating-and-naming-variables-dynamic/28665/2 "2008-04-28T15:14:37Z")

</div>

> Hi all,
> 
> I want to create a variable dynamic. I have defined some classes and depending on data I get from a database I need to construct an object eg:
> 
> now I do
> 
> if type=="car" variab = Car.new elsif type="boat" variab = Boat.new end
> 
> can I do something like variab = Instance.new(type)

Not quite. but if you can construct a string containing the class  
name, the some\_string.constantize.new should work. I've no idea what you're doing, but activerecord's single table  
inheritance (STI) may be of interest.

> Following this: can I give dynamic variablenames? now I do if type=="car" car\_string = Car.new elsif type="boat" boat\_string = Boat.new end
> 
> can I do something like dynamicpart\_string = Instance.new(type)

not cleanly (and my instinct is that if you do this you will be  
entering a world of pain)

Fred

---

<div class="post-metadata">

**Author:** ![Walter\_Lockhart](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@Walter\_Lockhart](https://discuss.rubyonrails.org/u/Walter_Lockhart)\
**Post date:** [April 28, 2008, 6:57pm UTC](https://discuss.rubyonrails.org/t/creating-and-naming-variables-dynamic/28665/3 "2008-04-28T18:57:36Z")

</div>

Hi Stijn,

for dynamic variable names take a look at instance\_variable\_set:

inst\_var = "wallyworld" instance\_variable\_set("@" + inst\_var, value)

which assigns some value, 'value' to the variable '@wallyworld'

and instance\_variable\_get:

value = instance\_variable\_get("@" + inst\_var)

which retrieves the value 'value' from the variable '@wallyworld'

Regards

Walter
