question: referencing a variable with a variable

K

knohr

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
 
K

knohr

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

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

Phrogz

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 = { :eek:bj => [1,2,3] } # Hash with a key that points to an array
bar = { :eek:bj => foo } # pointing to the other hash

....but you can still swap out what the variable foo points to and
the :eek:bj 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).
 
M

misaka

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.
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"}
}

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
 
E

Erik Hollensbe

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

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.
 
R

Robert Dober

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}
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top