[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

question: referencing a variable with a variable

knohr

6/13/2007 9:27:00 PM

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

5 Answers

knohr

6/13/2007 9:50:00 PM

0

On Jun 13, 2:26 pm, knohr <just_a_techie2...@yahoo.com> 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.
>
> 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/

Gavin Kistner

6/13/2007 10:22:00 PM

0

On Jun 13, 3:26 pm, knohr <just_a_techie2...@yahoo.com> 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).

misaka@pobox.com

6/14/2007 11:12:00 AM

0

On Jun 13, 10:26 pm, knohr <just_a_techie2...@yahoo.com> 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.

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

Erik Hollensbe

6/30/2007 9:41:00 AM

0

On 2007-06-13 14:26:55 -0700, knohr <just_a_techie200x@yahoo.com> said:

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


Robert Dober

6/30/2007 11:51:00 AM

0

On 6/13/07, knohr <just_a_techie200x@yahoo.com> 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.
>
> 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
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck