#name of instance

i’m back at trying to get a form to be automagically update from changes
to an associated object. this is the first time i’ve really started to
hit dead ends with ruby…

is there any way to get the name of an instance variable?

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bob.myname

producing:

bobo

···


~transami

(") dobee dobee do…
\v/
^ ^

opps, slight typo. not bob, but bobo:

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bobo.myname # OOPS

producing:

bobo

···

On Fri, 2002-07-19 at 01:07, Tom Sawyer wrote:

i’m back at trying to get a form to be automagically update from changes
to an associated object. this is the first time i’ve really started to
hit dead ends with ruby…

is there any way to get the name of an instance variable?

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bob.myname

producing:

bobo


~transami

(") dobee dobee do…
\v/
^ ^


~transami

(") dobee dobee do…
\v/
^ ^

bobo = A.new
bob.myname

If I write this

  baba = bobo = A.new

What is the "name" of the instance ?

Same question with

  A.new.myname

Guy Decoux

Tom Sawyer wrote:

i’m back at trying to get a form to be automagically update from changes
to an associated object. this is the first time i’ve really started to
hit dead ends with ruby…

is there any way to get the name of an instance variable?

What you appear to be asking for, is the name of the local variable
(bobo) that holds the reference to the object on which the method is
being called. I do not think this is possible, since it could be that
there wasn’t a local variable holding a reference when you called it
(such as if you called A.new.myname).

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bob.myname

Even with the name of a local variable in the outside scope, you’ll need
a binding to that scope to access it. A little more code showing what
you want to do with the name of the local variable would probably spawn
more concrete, useful answers than this one.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

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
reference it.

Second, there could be any number
of variables referencing the same
object.

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.

Cheers,
Hal

···

----- Original Message -----
From: “Tom Sawyer” transami@transami.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 19, 2002 2:23 AM
Subject: Re: #name of instance

opps, slight typo. not bob, but bobo:

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bobo.myname # OOPS

producing:

bobo

good points, quite right. your notions did make me rethink and…

i think i found a solution. instead of assocciating a widget to the name
of a variable, as i had been thinking, instead i must associate it to
the object itself. thus instead of the name, i will simply use the id.

thanks guys!

~transami

···

On Fri, 2002-07-19 at 01:40, Hal E. Fulton wrote:

----- Original Message -----
From: “Tom Sawyer” transami@transami.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 19, 2002 2:23 AM
Subject: Re: #name of instance

opps, slight typo. not bob, but bobo:

class A
def myname
puts self.name # ?
end
end

bobo = A.new
bobo.myname # OOPS

producing:

bobo

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
reference it.

Second, there could be any number
of variables referencing the same
object.

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.

Cheers,
Hal


~transami

(") dobee dobee do…
\v/
^ ^