From: Mark Bush Date: 2008-03-24T03:30:16+09:00 Subject: Re: Sytem hook for instance names? Jeff Patterson wrote: > Is there a system hook that allows a method to get an instance's symbol > from its' object id? > > i.e. > > a=SomeObject.new > SomeSystemhook(a) => :a (or "a" I suppose) The problem is that the variable is not the object, just a pointer to it. And there can be multiple variables pointing to the object. So: a = SomeObject.new b = a Now "a" and "b" both point to the same object. How would you choose between returning :a and :b? If you don't mind, then for instance variables you can do something like: def get_instance_variable_symbol_for_object v instance_variables.each do |var| if value = instance_variable_get(var) return var if value.object_id.eql?(v.object_id) end end end Untested. -- Posted via http://www.ruby-forum.com/.