Tim Pease
1/30/2007 3:56:00 PM
On 1/30/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:
>
> Hi all,
>
> 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
>
>
> Kind regards,
>
> Gerald Ebberink
>
My gut reaction is to say "no, you can't do that." The say_hi method
does not know about the context of the caller and the local variables
available there.
However, take a look at Ryan Davis' ParseTree and Ruby2Ruby gems.
These allow a program to analyze its own source code. That might be
one direction to look into.
So, a reserved "maybe" is probably the best answer ;)
Ryan or Eric, any thoughts on this one?
Blessings,
TwP