On Sep 18, 4:59=A0pm, Rick Tan <bellc... / hotmail.com> wrote:
> Is there a way in Ruby to use the content of a variable as the name of a
> variable. =A0For example
>
> var1 =3D 'myVar'
>
> i want to assign a value to 'myVar' by referencing var1. =A0The name of
> the variable can vary depending on the content of var1. =A0Other
> application include using a variable to hold the name of a class method
> which i need to call in my code.

With eval you can do quite a bit:

=3D=3D=3D=3D

v_name =3D "my_var"
v_value =3D "5"

eval "#{v_name} =3D #{v_value}"
eval "puts my_var"

# note: generates
error
puts my_var rescue puts "error"

my_var =3D 0  # now initialize the variable in current
binding

eval "#{v_name} =3D #{v_value}"
eval "puts my_var"

# no error
now
puts my_var

=3D=3D=3D=3D

Eric

=3D=3D=3D=3D

Are you interested in on-site Ruby or Ruby on Rails training
that uses well-designed, real-world, hands-on exercises?
http://LearnRuby.com