Dynamic Variable Names

Discussion in 'Ruby' started by Chris Martin, Dec 16, 2006.

  1. Chris Martin

    Chris Martin Guest

    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/
    Chris Martin, Dec 16, 2006
    #1
    1. Advertising

  2. 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/
    >
    >
    Steve Midgley, Dec 16, 2006
    #2
    1. Advertising

  3. 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/
    >
    >
    Steve Midgley, Dec 16, 2006
    #3
  4. Chris Martin

    George Ogata Guest

    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.
    George Ogata, Dec 16, 2006
    #4
  5. Chris Martin

    George Ogata Guest

    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.
    ;-)
    George Ogata, Dec 16, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,273
    cesarjusto
    Jul 15, 2010
  2. wanwan
    Replies:
    3
    Views:
    415
    Alex Martelli
    Oct 14, 2005
  3. News123
    Replies:
    2
    Views:
    457
    John Machin
    Nov 26, 2008
  4. Steve Holden

    Re: Dynamic variable names

    Steve Holden, Feb 7, 2010, in forum: Python
    Replies:
    0
    Views:
    372
    Steve Holden
    Feb 7, 2010
  5. Peter Buckley
    Replies:
    5
    Views:
    194
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page