Without some tremendous gyrations,
it’s just not possible.
Remember that a variable is just a
reference to an object.
First of all, an object has no way
of knowing what variable might
Second, there could be any number
of variables referencing the same
toto = gogo = bobo = A.new
toto.myname # Logically, all three
gogo.myname # of these must produce
bobo.myname # the same result.
Thirdly, there might be no variable
at all referencing the object.
A.new.myname # What now?
So unless you’re prepared to do some
weird things to associate a name with
an object when it’s created, there’s
just no way. Even if you do that,
there are still shortcomings.
Perhaps time to rethink.
If you’re not familiar with how Symbols
work in Ruby, go look that up. That
might give you some ideas.
----- Original Message -----
From: “Tom Sawyer” email@example.com
To: “ruby-talk ML” firstname.lastname@example.org
Sent: Friday, July 19, 2002 2:23 AM
Subject: Re: #name of instance
opps, slight typo. not bob, but bobo:
puts self.name # ?
bobo = A.new
bobo.myname # OOPS