From: Phrogz Date: 2007-01-31T02:10:08+09:00 Subject: Re: Accesing the name of an instance from within an method On 1/30/07, Gerald Ebberink wrote: > I am wondering if it is possible to access the name of an instance > from within a method. > What I would like to do is something similar to this > class Someclass > def say_hi > puts "Hi I am" + self.name > end > end > > fun = Someclass.new > fun.say_hi > > give the output > > Hi I am fun Instances do not have names; what you have above is a variable (named 'fun') that references your instance. For example, consider this: my_array = [ Someclass.new ] wow = my_array[ 0 ] my_hash = { :foo=>my_array[0] } wow.say_hi my_array[ 0 ].say_hi hy_hash[ :foo ].say_hi What would you expect that method to output?