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.

···

--
Chris Martin
Web Developer
Open Source & Web Standards Advocate
http://www.chriscodes.com/

Hi Chris,

I'm not positive I understand precisely what you want, but if you want to store/access a variable via a String values, you can do this via "Module::eval" or other similar techniques:

def test
   static = "static"
   dyno = "_dynamic1"
   data = "store me"
   eval(static+dyno+"='"+data+"'")
   # call it via hard code
   #call it dynamically
   eval("puts "+static+dyno)
end

test

I think this gets you where you want to be. For some reason, calling "static_dynamic1" hard coded wasn't working for me but if you need to go both ways, I'm sure it's possible. Not also the weird use of quote marks in the eval code. You have to quote "one layer" deeper than the code - but that's probably the same PHP

Best,

Steve

···

At 07:00 PM 12/15/2006, Chris Martin 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"
?>

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

Thanks.

--
Chris Martin
Web Developer
Open Source & Web Standards Advocate
http://www.chriscodes.com/

Hi Chris,

I'm not positive I understand precisely what you want, but if you want to store/access a variable via a String values, you can do this via "Module::eval" or other similar techniques:

def test
   static = "static"
   dyno = "_dynamic1"
   data = "store me"
   eval(static+dyno+"='"+data+"'")
   # call it via hard code
   #call it dynamically
   eval("puts "+static+dyno)
end

test

I think this gets you where you want to be. For some reason, calling "static_dynamic1" hard coded wasn't working for me but if you need to go both ways, I'm sure it's possible. Not also the weird use of quote marks in the eval code. You have to quote "one layer" deeper than the code - but that's probably the same PHP

Best,

Steve

···

At 07:00 PM 12/15/2006, Chris Martin 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"
?>

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

Thanks.

--
Chris Martin
Web Developer
Open Source & Web Standards Advocate
http://www.chriscodes.com/

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.

···

On 12/16/06, Chris Martin <chriscodes@gmail.com> 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"
?>

I believe this is because local variable-ness is determined
statically, so it won't recognize 'a' as a local unless it was a local
before the eval. Hence:

g@crash:~$ ruby -e "eval('a = 1'); p a"
-e:1: undefined local variable or method `a' for main:Object (NameError)
g@crash:~$ ruby -e "a = nil; eval('a = 1'); p a"
1

evalling the 'a', OTOH, causes local variable-ness to be checked when
the eval is run, so:

g@crash:~$ ruby -e "eval('a = 1'); p a"
-e:1: undefined local variable or method `a' for main:Object (NameError)
g@crash:~$ ruby -e "eval('a = 1'); p eval('a')"
1

I know you were just answering the OP's question. I tend to think all
this evalling business should really be avoided to begin with, though.
:wink:

···

On 12/16/06, Steve Midgley <public@misuse.org> wrote:

Hi Chris,

def test
   static = "static"
   dyno = "_dynamic1"
   data = "store me"
   eval(static+dyno+"='"+data+"'")
   # call it via hard code
   #call it dynamically
   eval("puts "+static+dyno)
end

test

I think this gets you where you want to be. For some reason, calling
"static_dynamic1" hard coded wasn't working for me but if you need to
go both ways, I'm sure it's possible.