Variable name

Is there a way in Ruby to use the content of a variable as the name of a
variable. For example

var1 = 'myVar'

i want to assign a value to 'myVar' by referencing var1. The name of
the variable can vary depending on the content of var1. Other
application include using a variable to hold the name of a class method
which i need to call in my code.

Thanks in advance

···

--
Posted via http://www.ruby-forum.com/.

Rick Tan wrote:

Is there a way in Ruby to use the content of a variable as the name of a
variable. For example

var1 = 'myVar'

i want to assign a value to 'myVar' by referencing var1. The name of
the variable can vary depending on the content of var1. Other
application include using a variable to hold the name of a class method
which i need to call in my code.

Thanks in advance

In general, no. Most of the time this problem can be solved by using a hash. That is,

var1 = 'myVar'
hsh[var1] = value

In the specific case of calling a method whose name is in a variable, use send:

var1 = 'my_method'
obj.send(var1, arg1, arg2,...)

···

--
RMagick: http://rmagick.rubyforge.org/

With eval you can do quite a bit:

···

On Sep 18, 4:59 pm, 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. For example

var1 = 'myVar'

i want to assign a value to 'myVar' by referencing var1. The name of
the variable can vary depending on the content of var1. Other
application include using a variable to hold the name of a class method
which i need to call in my code.

====

v_name = "my_var"
v_value = "5"

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

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

my_var = 0 # now initialize the variable in current
binding

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

# no error
now
puts my_var

====

Eric

====

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

Rick Tan wrote:

Is there a way in Ruby to use the content of a variable as the name of a
variable. For example

var1 = 'myVar'

i want to assign a value to 'myVar' by referencing var1. The name of
the variable can vary depending on the content of var1.

If you're talking about "myVar" being the name of a *local* variable,
then usually this means you're trying to solve the wrong problem :slight_smile:

The normal solution is for var1 to refer to one of the following.

(1) an instance variable of an object

  var1 = '@foo'
  val = obj.instance_variable_get(var1)
  obj.instance_variable_set(var1, val + 1)

(You can omit 'obj.' if you're talking about instance variables of the
'self' object)

(2) a method name

  var1 = "foo"
  obj.send(var1, 2, 3) # same as obj.foo(2,3)

  var1 = "foo="
  obj.send(var1, 4) # same as obj.foo = 4

(3) a constant or a class name

  var1 = "String"
  klass = Object.const_get(var1)
  str = klass.new # same as str = String.new

  require 'net/http'
  var1 = "HTTP"
  klass = Net.const_get(var1)
  klass.new(...) # same as Net::HTTP.new(...)

Other
application include using a variable to hold the name of a class method
which i need to call in my code.

For that you use "send":

  MyClass.send(var1, arg, arg...)

HTH,

Brian.

···

--
Posted via http://www.ruby-forum.com/\.