From: Adam Prescott Date: 2011-04-05T21:50:07+09:00 Subject: Re: Autoconvert object from one class1 to other class2 in class1 def --bcaec51a833ebae21204a02b5061 Content-Type: text/plain; charset=UTF-8 On Tue, Apr 5, 2011 at 10:15 AM, Luk Mus wrote: > Brian Candler wrote in post #990955: > >> or how get name of variable in class method? > > > > That's not possible either. The same object could be pointed to by many > > variables (local and/or global and/or instance), or by none. For > > example, maybe the only reference to the object is as a value in a Hash. > > Thanks! > > But i can get object_id: > > class String > def get_id > puts self.__id__ > puts self.object_id > end > end > > Is it realy to get name by ID? I suppose just for the local variable case, you could use Kernel's `local_variables`, combined with a binding, and having a single reference to the object you're trying to find all local variable references to. ("_" here is because of irb.) Equivalent gist paste for the below: https://gist.github.com/903529 >> local_variables => ["_"] >> a = "" => "" >> local_variables => ["_", "a"] >> b = binding; local_variables.select { |var| b.eval(var).object_id == a.object_id } => ["a"] >> x = a => "" >> local_variables => ["_", "a", "b", "x"] >> b = binding; local_variables.select { |var| b.eval(var).object_id == a.object_id } => ["a", "x"] Not a very useful exercise, though, I think, more an interesting one. --bcaec51a833ebae21204a02b5061--