# Variable name

**URL:** <https://www.ruby-forum.com/t/variable-name/148985>\
**Category:** Ruby\
**Created:** [September 18, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/variable-name/148985 "2008-09-18T23:07:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 18, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/variable-name/148985/1 "2008-09-18T23:07:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 18, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/variable-name/148985/2 "2008-09-18T23:35:58Z")

</div>

Rick T. wrote:

> 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,…)

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 18, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/variable-name/148985/3 "2008-09-18T23:40:39Z")

</div>

On Sep 18, 4:59 pm, Rick T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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](http://LearnRuby.com)

---

<div class="post-metadata">

**Author:** ![bellcolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@bellcolt](https://www.ruby-forum.com/u/bellcolt)\
**Post date:** [September 19, 2008, 10:11am UTC](https://www.ruby-forum.com/t/variable-name/148985/4 "2008-09-19T10:11:12Z")

</div>

Rick T. 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 🙂

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.
