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?
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).
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.
Of course, that assumes you want to test for equality. If you want to make
for subtle bugs that only show up sometimes.
Yeah, good summary of all the drawbacks of this approach.
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.
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.
On Wednesday, May 19, 2010 01:53:58 pm Robert K. wrote:
other objects.
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.
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 :@test, t
end
sleep 30
end
end
def find_con con
con.instance_variable_get :@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.
Just do what Robert Klein said.
Erm, who?
Whoops… Actually, what is the etiquette on a forum like this? Should I
just
say “Robert”?
On Wednesday, May 19, 2010 01:53:58 pm Robert K. wrote:
Just do what Robert Klein said.
Erm, who?
Whoops… Actually, what is the etiquette on a forum like this? Should I just
say “Robert”?
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:
Unlike Robert Klein - Wikipedia who I can’t recall
ever seeing around these parts, Robert K. CAN stop his leg, as far
as I know.
Actually I had to look around a bit since I never heard of this
namesake of mine. I found
Hehe. You know what? That’s the typical phrase I use when
someone does not know how to spell my surname.
http://www.economicexpert.com/a/LEO:computer.html Amazon.co.uk Amazon.co.uk
… 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. …
Amazing! Learn something new every day. Thanks for that!
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.