From: Gary Wright Date: 2012-02-21T10:35:38+09:00 Subject: Re: Accessing a local variable through a symbol On Feb 20, 2012, at 8:13 PM, Darryl L. Pierce wrote: > On Mon, Feb 20, 2012 at 8:03 PM, Ralph Shnelvar wrote: >> Darryl, >> >> Monday, February 20, 2012, 1:57:53 PM, you wrote: >> >> DLP> On Mon, Feb 20, 2012 at 3:49 PM, Ralph Shnelvar wrote: >>>> local_variables delivers a list of symbols of the local variables. I'm trying to access the underlying values associated with that list. >> >> DLP> def foo >> DLP> x = 5 >> DLP> y = 9 >> 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. 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 Wright