variable name

R

Rick Tan

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
 
T

Tim Hunter

Rick said:
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,...)
 
E

Eric I.

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.

With eval you can do quite a bit:

====

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
 
B

Brian Candler

Rick said:
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 :)

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.
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top