From: rubisher Date: 2008-05-24T05:39:07+09:00 Subject: Re: Is it possible from a class to grab the name of the object instance? Robert Klemme wrote: > 2008/5/20 rubisher : >> Hello Marcin, >> >>> self.class.to_s should work >>> >>> >> Sorry this return me the class name itself not the instance name? >> >> e.g. >> #!/usr/bin/env ruby >> >> class Investment >> def initialize >> puts "I'am ALIVE!" >> iname=self.class.to_s >> puts "Instance Name: #{iname}" >> end >> end >> >> _Invest = Investment.new >> >> puts _Invest.class >> >> give me: >> I'am ALIVE! >> Instance Name: Investment >> Investment >> >> and not "Instance Name: _Invest" as am looking for. > > There is no such thing as an instance name - unless you define a > property of your class called "name". Ok what I did. The idea was that as far as ruby is interpreded, it have to save variable name somewhere in clear with its id and for debuging reason it could be accessible somehow. But ok it's not possible and for me it's just nice to know. > Each instance can be referenced > by any number of variables including 0 - there is simply not _the_ > name. As Inaki pointed out. > > Kind regards > > robert > Tx to all for feedback, r.