# Dynamic Variable Names

**URL:** <https://www.ruby-forum.com/t/dynamic-variable-names/79537>\
**Category:** Ruby\
**Created:** [December 16, 2006, 4:01am UTC](https://www.ruby-forum.com/t/dynamic-variable-names/79537 "2006-12-16T04:01:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [December 16, 2006, 4:01am UTC](https://www.ruby-forum.com/t/dynamic-variable-names/79537/1 "2006-12-16T04:01:05Z")

</div>

Is it possible to create dynamic variable names in ruby?

For example, in PHP it’s done like this

\<?php $a = 1; ${"myvar\_{$a}"} = "some\_value"; # $myvar\_1 = "some\_value" ?\>

I’m having a hard time finding anything relevant when searching, as  
‘dynamic’ and ‘variable’ are used quite frequently in text mentioning  
ruby.

Thanks.

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [December 16, 2006, 4:59am UTC](https://www.ruby-forum.com/t/dynamic-variable-names/79537/2 "2006-12-16T04:59:12Z")

</div>

On 12/16/06, Chris M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to create dynamic variable names in ruby?
> 
> For example, in PHP it’s done like this
> 
> \<?php $a = 1; ${"myvar\_{$a}"} = "some\_value"; # $myvar\_1 = "some\_value" ?\>

Hi Chris,

Couldn’t you just throw them in a hash?

my\_vars[a] = ‘some value’

It depends on what you’re doing, but this often leads to other nice  
things, like not having your “dynamic variable” names collide with  
others that happen to be in scope, and being able to iterate through  
them, inspect them, pass them around as a unit, etc.
