From: Robert Dober Date: 2007-05-06T20:55:49+09:00 Subject: Re: How can I output an object/variable's name? On 5/6/07, John Joyce wrote: > maybe it's not necessary to do this. I want the instance's name (like > p1 here) to be accessible and manipulatable like an attribute as well. > Maybe there is a slight missunderstanding between local variables and instance_variables Look at this 528/28 > cat inst-vars.rb && ruby inst-vars.rb # vim: sts=2 sw=2 tw=0 expandtab nu: # @a = 42 @b = "fourtytwo" @c = {:a=>42,:b=>22} @d = Object.new instance_variables.each do |ivar| puts "instance variable #{ivar} refers to object: " << instance_variable_get(ivar).inspect end instance variable @d refers to object: # instance variable @b refers to object: "fourtytwo" instance variable @a refers to object: 42 instance variable @c refers to object: {:b=>22, :a=>42} Yet there is still no way to go from the object to the instance variable. And yes you can achieve about the same for local variables with local_variables.each do |lvar| value = eval(lvar) ... end I see why you needed eval now!! Cheers Robert > -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw