From: Erik Hollensbe Date: 2007-06-30T18:45:00+09:00 Subject: Re: question: referencing a variable with a variable On 2007-06-13 14:26:55 -0700, knohr 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.