# Create dynamic variable

**URL:** <https://www.ruby-forum.com/t/create-dynamic-variable/140947>\
**Category:** Ruby\
**Created:** [June 20, 2008, 12:52am UTC](https://www.ruby-forum.com/t/create-dynamic-variable/140947 "2008-06-20T00:52:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [June 20, 2008, 12:52am UTC](https://www.ruby-forum.com/t/create-dynamic-variable/140947/1 "2008-06-20T00:52:45Z")

</div>

Hi !

is it possible in ruby to create dynamic variables like in php by doing  
$b=$$a ??

i’d like to infer an instance variable from a layout view, in order to  
set up dynamically a precise id to my html object (so i need the name  
of the current objetc + its id)

Thanks in advance

nico

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [June 20, 2008, 8:26am UTC](https://www.ruby-forum.com/t/create-dynamic-variable/140947/2 "2008-06-20T08:26:44Z")

</div>

On 20.06.2008 00:51, nico Itkin wrote:

> is it possible in ruby to create dynamic variables like in php by doing  
> $b=$$a ??
> 
> i’d like to infer an instance variable from a layout view, in order to  
> set up dynamically a precise id to my html object (so i need the name  
> of the current objetc + its id)

This is usually an indication that you rather want a Hash.

Having said that here are a few other options you have:

OpenStruct  
instance\_variable\_get/set  
eval (not recommended)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [June 20, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/create-dynamic-variable/140947/3 "2008-06-20T16:41:41Z")

</div>

On Jun 20, 3:23 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

This was the way I dicovered arrays back when I was 11 years old. I  
kept trying to concatenate a number with a fix variable name in some  
version of basic.

Lucas.
