# Variable variables

**URL:** <https://www.ruby-forum.com/t/variable-variables/126350>\
**Category:** Rails\
**Created:** [January 17, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/variable-variables/126350 "2008-01-17T17:13:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spokra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@spokra](https://www.ruby-forum.com/u/spokra)\
**Post date:** [January 17, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/variable-variables/126350/1 "2008-01-17T17:13:18Z")

</div>

I know this can be done… rails creates methods from variables all the  
time… but how do you code it?

say you have two strings.

a=“test”  
test=“this is a test”

how do you get the contents of test with the variable a?

#{a}

well that doesn;t work!! I know this is a easy question for the ruby  
gods out there… but for a newbie learning the language it’s  
hard… :\> HELP!!!

---

<div class="post-metadata">

**Author:** ![spokra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@spokra](https://www.ruby-forum.com/u/spokra)\
**Post date:** [January 17, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/variable-variables/126350/2 "2008-01-17T18:14:58Z")

</div>

> say you have two strings.
> 
> a=“test”  
> test=“this is a test”
> 
> how do you get the contents of test with the variable a?

Go read “The Ruby Way”

class A  
attr\_accessor :variable, :test  
end

a = A.new

a.variable = :test  
a.send a.variable if a.responds\_to? a.variable

send is a method in class Object that takes a symbol and an argument  
list. the symbol is used to call a method of a corresponding name which  
is given the arguments.

Rails tends to use this in combination with method missing in active  
record for its handling of database attributes which I assume is what  
your referring to.

-K

---

<div class="post-metadata">

**Author:** ![spokra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@spokra](https://www.ruby-forum.com/u/spokra)\
**Post date:** [January 29, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/variable-variables/126350/3 "2008-01-29T17:13:58Z")

</div>

a = “test”  
test = “this is a test”  
eval a # =\> “this is a test”

---

<div class="post-metadata">

**Author:** ![spokra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@spokra](https://www.ruby-forum.com/u/spokra)\
**Post date:** [January 29, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/variable-variables/126350/4 "2008-01-29T17:29:52Z")

</div>

Or depending on what you’re doing, why not do this:

a = “This is my #{test} variable.”  
test = “super cool”

-Ryan Kopf
