Dynamic Variable Names

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.

On 12/16/06, Chris M. [email protected] 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.