create dynamic variable

Discussion in 'Ruby' started by nico Itkin, Jun 19, 2008.

  1. nico Itkin

    nico Itkin Guest

    Hi !

    is it possible in ruby to create dynamic variables like in php by doing
    $b=$$a ??

    i'd like to infer an instance variable from a layout view, in order to
    set up dynamically a precise id to my html object (so i need the name
    of the current objetc + its id)

    Thanks in advance

    nico
    --
    Posted via http://www.ruby-forum.com/.
     
    nico Itkin, Jun 19, 2008
    #1
    1. Advertising

  2. On 20.06.2008 00:51, nico Itkin wrote:
    > is it possible in ruby to create dynamic variables like in php by doing
    > $b=$$a ??
    >
    > i'd like to infer an instance variable from a layout view, in order to
    > set up dynamically a precise id to my html object (so i need the name
    > of the current objetc + its id)


    This is usually an indication that you rather want a Hash.

    Having said that here are a few other options you have:

    OpenStruct
    instance_variable_get/set
    eval (not recommended)

    Kind regards

    robert
     
    Robert Klemme, Jun 20, 2008
    #2
    1. Advertising

  3. nico Itkin

    Guest

    On Jun 20, 3:23 am, Robert Klemme <> wrote:
    > On 20.06.2008 00:51, nico Itkin wrote:
    >
    > > is it possible in ruby to create dynamic variables like in php by doing
    > > $b=$$a ??

    >
    > > i'd like to infer an instance variable from a layout view, in order to
    > > set up dynamically a precise  id to my html object (so i need the name
    > > of the current objetc + its id)

    >
    > This is usually an indication that you rather want a Hash.
    >


    This was the way I dicovered arrays back when I was 11 years old. I
    kept trying to concatenate a number with a fix variable name in some
    version of basic.

    Lucas.
     
    , Jun 20, 2008
    #3
    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. Brian
    Replies:
    3
    Views:
    450
    Siemel Naran
    Apr 19, 2004
  2. tirrell payton

    how to create a dynamic email from variable

    tirrell payton, Jan 20, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    395
    Nitin
    Jan 23, 2007
  3. mfglinux
    Replies:
    11
    Views:
    754
    Roberto Bonvallet
    Sep 12, 2007
  4. Peter Buckley
    Replies:
    5
    Views:
    225
    matt neuburg
    Feb 27, 2009
  5. David Filmer
    Replies:
    19
    Views:
    282
    Kevin Collins
    May 21, 2004
Loading...

Share This Page