# Question: referencing a variable with a variable

**URL:** <https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771>\
**Category:** Ruby\
**Created:** [June 13, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771 "2007-06-13T23:32:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 13, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/1 "2007-06-13T23:32:42Z")

</div>

I come from a perl background. I have gotten in the habit of using  
references to variables instead of specifically calling said variable/  
class/whatever.

Basically, I am wondering how I would translate the following into  
ruby.

example 1:  
sub my\_sub  
{  
$choice = shift;  
$choices=“this|that|foo|bar”;  
if ( $choice =~ m/($choices)/ ) {&$choice}  
else {die “some witty msg”}  
}

example 2:

$this = ‘var\_name’;  
$var\_name = ‘i want this to print’;  
print $$var\_name . “\n”;

thanks

---

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 13, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/2 "2007-06-13T23:50:44Z")

</div>

On Jun 13, 2:26 pm, knohr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $choice = shift;
> 
> thanks

s/print $$var\_name/print $$this/

---

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 14, 2007, 12:25am UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/3 "2007-06-14T00:25:49Z")

</div>

On Jun 13, 3:26 pm, knohr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I come from a perl background. I have gotten in the habit of using  
> references to variables instead of specifically calling said variable/  
> class/whatever.

In Ruby, a variable is a reference to an object. While you can create  
multiple references to the same object, you cannot create a reference  
to a variable. You can create an object that refers to another object:

foo = { :obj =\> [1,2,3] } # Hash with a key that points to an array  
bar = { :obj =\> foo } # pointing to the other hash

…but you can still swap out what the variable foo points to and  
the :obj key in bar will not be updated.

You can, however, look up instance and class variables by name:  
@foo1 = “whee”  
@foo2 = “la”  
answer = ‘@foo1’  
puts instance\_variable\_get( answer )

I think that’s about as close as you’re going to get (without using  
eval).

---

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 14, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/4 "2007-06-14T13:16:13Z")

</div>

On Jun 13, 10:26 pm, knohr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I come from a perl background. I have gotten in the habit of using  
> references to variables instead of specifically calling said variable/  
> class/whatever.

I don’t think that symbolic references directly supported in Ruby, but  
as Phrogz pointed out there are certain methods to access variables  
and methods by using a string representation of their name.

> else {die “some witty msg”}  
> }

This one is easy, there’s a “send” method that let’s you call a method  
using a string containing it’s name. Here’s an abbreviated version of  
what you have above:

def my\_sub( $choice )  
if $choice =~ /foo|bar/  
send( $choice )  
end  
end

> example 2:
> 
> $this = ‘var\_name’;  
> $var\_name = ‘i want this to print’;  
> print $$var\_name . “\n”;

Looks like this would probably need an eval:

this = ‘var\_name’  
var\_name = ‘i want this to print’  
puts( eval( this ) )

There are methods to list local variables ‘local\_variables’, and  
return instance variables, but there isn’t really anything to actually  
return the value of a local variable so far as I can see.

–Misha

---

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 30, 2007, 11:46am UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/5 "2007-06-30T11:46:13Z")

</div>

On 2007-06-13 14:26:55 -0700, knohr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> $choice = shift;
> 
> thanks

Hi Knohr. 🙂

There are no soft references in ruby. However, you can use symbols to  
accomplish most of what you’re trying to do:

choice = “foo”  
if choice =~ /foo|bar|baz/  
method(choice.to\_sym).call  
end

See Object#\*\_variable\_get for ways to get at variables.

---

<div class="post-metadata">

**Author:** ![knohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@knohr](https://www.ruby-forum.com/u/knohr)\
**Post date:** [June 30, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/question-referencing-a-variable-with-a-variable/106771/6 "2007-06-30T13:52:20Z")

</div>

On 6/13/07, knohr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $choice = shift;  
> $choices=“this|that|foo|bar”;  
> if ( $choice =~ m/($choices)/ ) {&$choice}  
> Sorry if I am asking something stupid here but my $perl 😉 days are  
> long gone now.  
> You are matching the reference of a sub with a rgexp and then call the  
> sub you are holding the reference of???

Assuming that you meant something like  
if (whatever) {&$choice}  
you will write  
if whatever choice.call

The interesting difference will be the call of my\_sub, which I have to  
call my\_def in Ruby ;).

def my\_def callable

#now callable could be a lambda  
my\_def lambda{ puts 42 }  
#a method  
s=“\*” \* 42  
m = s.method(:size)  
my\_def m

But very probably you want to use something more Rubish.  
def my\_def …  
yield if something # with many variations available  
Proc.new.call if something

The next approach – my preferred – will give you a reference to the  
block - a little bit like an anonymous sub in perl.

def my\_def …, &blk  
blk.call if something  
end

> example 2:
> 
> $this = ‘var\_name’;  
> $var\_name = ‘i want this to print’;  
> print $$var\_name . “\n”;
> 
> thanks  
> this = “var\_name”  
> var\_name = “I want #{this} to print”  
> puts var\_name

I am aware of the conceptional difference – and that was nicely  
explained already 🙂 - but maybe that is all you need 😉

HTH  
Robert
