Question: referencing a variable with a variable

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

On Jun 13, 2:26 pm, knohr [email protected] wrote:

$choice = shift;

thanks

s/print $$var_name/print $$this/

On Jun 13, 3:26 pm, knohr [email protected] 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).

On Jun 13, 10:26 pm, knohr [email protected] 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

On 2007-06-13 14:26:55 -0700, knohr [email protected] said:

$choice = shift;

thanks

Hi Knohr. :slight_smile:

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.

On 6/13/07, knohr [email protected] wrote:

$choice = shift;
$choices=“this|that|foo|bar”;
if ( $choice =~ m/($choices)/ ) {&$choice}
Sorry if I am asking something stupid here but my $perl :wink: 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 :slight_smile: - but maybe that is all you need :wink:

HTH
Robert