From: John Joyce Date: 2007-05-06T13:04:02+09:00 Subject: Re: How can I output an object/variable's name? On May 6, 2007, at 9:41 AM, Gary Wright wrote: > > On May 5, 2007, at 6:02 PM, Rick DeNatale wrote: >> On 5/5/07, John Joyce wrote: >>> What I'd like to do is similar to the idea of processing files and >>> outputing results with names of files. >>> I want to do the same with objects. >>> I wrote an little code block just to talk back to me and tell me if >>> an object is tainted or not. ("I'm making it plain English >>> sentences, >>> rather than simply true or false) >> [..] >> Variables are just names for objects, and aren't properties of the >> objects themselves. Objects don't know what folks are calling them. > > You can't go from an arbitrary object reference to an identifier, but > you can go from an identifier to an object reference via eval: > > a = 42 > @foo = 'bar' > ["a", "@foo", "Array"].each { |id| > puts "#{id} references a #{eval(id).class}" > } > > # output > > a references a Fixnum > @foo references a String > Array references a Class > > > > Gary Wright Gary I think this does what I'm looking for. It's a little hackish to me, but it seems to work. I find eval to be one of those less obvious tools. I was just reading up on eval and its cadre of similar methods last night. A very mystical bunch to me still.