Dynamic Variable Names

C

Chris Martin

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.
 
S

Steve Midgley

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
 
S

Steve Midgley

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
 
G

George Ogata

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.
 
G

George Ogata

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.

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.
;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top