Accessing a local variable through a symbol

Let’s say I have

def y
a=42
x = some_kernel_function(:a) # I’d like x to be 42
end

Is there a way to get x to be 42?

On Mon, Feb 20, 2012 at 3:27 PM, Ralph S. [email protected]
wrote:

Let’s say I have

def y
a=42
x = some_kernel_function(:a) # I’d like x to be 42
end

Is there a way to get x to be 42?

What, exactly, is it you’re trying to do? What is
“some_kernel_function”?

Since a in the above is a local variable, why can’t you just assign it
directly?

x = a

Or are you trying to do something like access the most recent value
processed or something like that?

Darryl,

Monday, February 20, 2012, 1:35:10 PM, you wrote:

Darryl,

Monday, February 20, 2012, 1:57:53 PM, you wrote:

On Mon, Feb 20, 2012 at 8:03 PM, Ralph S. [email protected]
wrote:

DLP> z = 9

DLP> local_variables.each { |varname| puts “#{varname} evaluates to
DLP> #{eval varname}”}
DLP> end

DLP> Try that.

I get
in `eval’: can’t convert Symbol into String (TypeError)

I didn’t use any symbols. Please post the exact code you’re using.

Darryl,

Monday, February 20, 2012, 6:13:46 PM, you wrote:

On Mon, Feb 20, 2012 at 3:49 PM, Ralph S. [email protected]
wrote:

local_variables delivers a list of symbols of the local variables. I’m trying to
access the underlying values associated with that list.

def foo
x = 5
y = 9
z = 9

local_variables.each { |varname| puts “#{varname} evaluates to
#{eval varname}”}
end

Try that.

On Mon, Feb 20, 2012 at 5:13 PM, Darryl L. Pierce [email protected]
wrote:

DLP> y = 9

I didn’t use any symbols. Please post the exact code you’re using.

local_variables produces an array of symbols: #{eval varname} should
be #{eval varname.to_s}

On Feb 20, 2012, at 8:13 PM, Darryl L. Pierce wrote:

DLP> y = 9

I didn’t use any symbols. Please post the exact code you’re using.

In 1.9.X versions of Ruby, local_variables returns a list of symbols.
In older versions it returns the names as strings.

a = 3 #=> 3
b = 4 #=> 4
local_variables.each { |n| puts “#{n} is #{eval n.to_s}” } #=> [:b, :a]
b is 4
a is 3

Gary W.

On Mon, Feb 20, 2012 at 9:27 PM, Ralph S. [email protected]
wrote:

Let’s say I have

def y
a=42
x = some_kernel_function(:a) # I’d like x to be 42
end

Is there a way to get x to be 42?

It generally does not make sense: If you know all your local variables
you do not need a dynamic approach. If you need dynamic variables you
better stuff things in a Hash because you cannot create local
variables dynamically which could be seen. I am aware of only one use
case where it may actually make sense.

What are you trying to achieve?

Kind regards

robert

On Mon, Feb 20, 2012 at 8:32 PM, Ralph S. [email protected]
wrote:

I get
in `eval’: can’t convert Symbol into String (TypeError)

DLP> I didn’t use any symbols. Please post the exact code you’re using.

You implicitly used symbols because local_variables produces an array of
symbols.

Not in 1.8.7 it doesn’t. Since no specific version was mentioned, I
used what’s in Fedora.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs