Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dynamic Variable Names

Reply
Thread Tools

Dynamic Variable Names

 
 
Chris Martin
Guest
Posts: n/a
 
      12-16-2006
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/

 
Reply With Quote
 
 
 
 
Steve Midgley
Guest
Posts: n/a
 
      12-16-2006
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/
>
>




 
Reply With Quote
 
 
 
 
Steve Midgley
Guest
Posts: n/a
 
      12-16-2006
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/
>
>




 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      12-16-2006
On 12/16/06, 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"
> ?>


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.

 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      12-16-2006
On 12/16/06, Steve Midgley <> 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.


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic variable names without EVAL - NEWBIE jason@cyberpine.com ASP .Net 2 07-15-2010 02:39 PM
Re: Dynamic variable names Steve Holden Python 0 02-07-2010 01:31 PM
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57