Retrieve instance

W

Walle Wallen

Quick question. Can I somehow retrieve the instance of the class Test in
which the variable con exists. As in the example below, can I retrieve
the instance of Test from the variable p?

class Test
attr_accessor :con

def initialize()
@con = Hash.new
end

end

p = Test.new.con
 
R

Robert Klemme

2010/5/19 Walle Wallen said:
Quick question. Can I somehow retrieve the instance of the class Test in
which the variable con exists. As in the example below, can I retrieve
the instance of Test from the variable p?

There is no general mechanism that would allow to follow an object
reference backwards. If there was, that mechanism would yield
multiple instances because a object can be referenced by any number of
other objects.

Bottom line: you can only follow object references from the container
to the contained so you need to make sure you have the reference you
need (see below).
class Test
=A0 =A0attr_accessor :con

=A0 def initialize()
=A0 =A0 =A0 =A0@con =3D Hash.new
=A0 end

end

p =3D Test.new.con

t =3D Test.new
p =3D t.com

Btw, p might not be a good name for a local variable as it may lead to
confusion with method p.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Jesús Gabriel y Galán

Ah, I see.
Thank you for the enlightenment

You can also explicitly pass the container object into the contained:

class Contained
attr_reader :container
def initialize container
@container = container
end
end

class Test
attr_reader :con
def initialize
@con = Contained.new self
end
end

contained = Test.new.con
p contained.container

Jesus.
 
D

David Masover

There is no general mechanism that would allow to follow an object
reference backwards. If there was, that mechanism would yield
multiple instances because a object can be referenced by any number of
other objects.

Well, there's ObjectSpace. Probably not general (cross-interpreter), but it
does work if you actually need this functionality. However, ObjectSpace is
code smell -- if you're using ObjectSpace, either you _really_ know what
you're doing, or you probably need to rethink your design.

If you do need that, for some reason:

def find_con con
ObjectSpace.each_object(Test).select{|t| t.con == con}
end

Of course, that assumes you want to test for equality. If you want to make
sure they're actually the same object, you might do

def find_con con
ObjectSpace.each_object(Test).select{|t| t.con.object_id == con.object_id}
end

And of course, that will return an array. It will also not be particularly
fast, and it won't necessarily be reliable -- for example, if you don't have a
reference to the Test object anymore, it might be garbage collected before you
go looking for it, but it might not -- so there's all sorts of possibilities
for subtle bugs that only show up sometimes.

Just do what Robert Klein said.
 
R

Robert Klemme

2010/5/19 David Masover said:
Well, there's ObjectSpace. Probably not general (cross-interpreter), but = it
does work if you actually need this functionality.

Even with ObjectSpace you cannot really travel a reference backwards.
You're merely forced to look at all objects (of a kind) and figure
whether any of them references (forward) the one whose parent you are
looking for as your code nicely demonstrates. Actually I find that
approach so ridiculous for the case at hand that I didn't even want to
come up with it. :)
However, ObjectSpace is
code smell -- if you're using ObjectSpace, either you _really_ know what
you're doing, or you probably need to rethink your design.

If you do need that, for some reason:

def find_con con
=A0ObjectSpace.each_object(Test).select{|t| t.con =3D=3D con}
end

Of course, that assumes you want to test for equality. If you want to mak= e
sure they're actually the same object, you might do

def find_con con
=A0ObjectSpace.each_object(Test).select{|t| t.con.object_id =3D=3D con.ob= ject_id}
end

And of course, that will return an array. It will also not be particularl= y
fast, and it won't necessarily be reliable -- for example, if you don't h= ave a
reference to the Test object anymore, it might be garbage collected befor= e you
go looking for it, but it might not -- so there's all sorts of possibilit= ies
for subtle bugs that only show up sometimes.

Yeah, good summary of all the drawbacks of this approach. :)
Just do what Robert Klein said.

Erm, who?

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
D

David Masover

Even with ObjectSpace you cannot really travel a reference backwards.
You're merely forced to look at all objects (of a kind) and figure
whether any of them references (forward) the one whose parent you are
looking for as your code nicely demonstrates.

I suppose. I still tend to think of things like this, because the Internet is
actually eroding that kind of assumption. Once upon a time, I might have
considered having another identity online, but occasionally linking back to my
"real" identity. Basically, people who know me as SecretGuy could find out who
I actually am, but not the other way around, with the assumption that friends
and family from reality wouldn't be likely to stumble on SecretGuy.

But the idea of one-way links on the Web is just as absurd as the idea of one-
way links in SQL. The assumption that no one would ever look through the
entire Internet for my alter-egos, or stumble on one by chance, is destroyed
by the fact that Google already indexes the entire Internet, turning forward
references into backward references.

Something similar could be done with ObjectSpace, but it'd be even more
ridiculous:

Thread do
loop do
ObjectSpace.each_object(Test) do |t|
con.instance_variable_set :mad:__test__, t
end
sleep 30
end
end

def find_con con
con.instance_variable_get :mad:__test__
end

I'll leave it as an exercise to the reader why that's so bad, and how it might
be improved on.

But this can't be good for my sanity.
Erm, who?

Whoops... Actually, what is the etiquette on a forum like this? Should I just
say "Robert"?
 
R

Rick DeNatale

Whoops... Actually, what is the etiquette on a forum like this? Should I just
say "Robert"?

Unlike http://en.wikipedia.org/wiki/Robert_Klein who I can't recall
ever seeing around these parts, Robert Klemme CAN stop his leg, as far
as I know. <G>

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
R

Robert Klemme

That's perfectly OK for me.

Btw, funny thing is that "klein" is actually also a German word which
can be directly translated to "small". "Klemme" is a tad more
difficult, you can see translations here:
http://dict.leo.org/?search=klemme
Unlike http://en.wikipedia.org/wiki/Robert_Klein who I can't recall
ever seeing around these parts, Robert Klemme CAN stop his leg, as far
as I know. <G>

Actually I had to look around a bit since I never heard of this
namesake of mine. I found

Hehe. Movit, movit... I...I...I....can't stop - aaargh ;-)

Cheers

robert
 
C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

Btw, funny thing is that "klein" is actually also a German word which
can be directly translated to "small". "Klemme" is a tad more
difficult, you can see translations here:
http://dict.leo.org/?search=klemme
Google Translate seems quite close to that, giving several possibilities:
http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme<http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme>
but http://babelfish.yahoo.com gives just a one word answer "wedges"!
I rather like the phrase "in der Klemme sitzen" in "http://dict.leo.org",
and that "leo" reminds me of the following "quiz" question:
Was the world's first business computer manufactured by:
(a) IBM,
(b) another American business machines company, or
(c) a chain of British tea-shops and cafes?
The answer is of course (c)!
http://en.wikipedia.org/wiki/LEO_(computer)<http://en.wikipedia.org/wiki/LEO_(computer)>
http://www.leo-computers.org.uk
http://www.kzwp.com/lyons/index.htm <http://www.kzwp.com/lyons>
http://www.kzwp.com/lyons/leo.htm
http://www.economicexpert.com/a/LEO:computer.html
http://www.amazon.co.uk/Computer-Called-LEO-worlds-computer/dp/1841151858
http://www.amazon.co.uk/product-reviews/1841151866
... When I joined the computer industry in 1965, a friend in IBM confided in
me that the Leo (Lyons Electronic Office) series of computers (already in
there last days) were IBM's most serious rival in commercial computing. ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top