create dynamic variable

N

nico Itkin

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
 
R

Robert Klemme

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
 
K

krusty.ar

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top