Retrieve instance

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

···

--
Posted via http://www.ruby-forum.com/.

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
attr_accessor :con

def initialize()
@con = Hash.new
end

end

p = Test.new.con

t = Test.new
p = 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

···

2010/5/19 Walle Wallen <walle.sthlm@gmail.com>:

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

Ah, I see.
Thank you for the enlightenment

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Wednesday, May 19, 2010 09:54:03 am Robert Klemme wrote:

2010/5/19 Walle Wallen <walle.sthlm@gmail.com>:
> 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.

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.

···

On Wed, May 19, 2010 at 4:59 PM, Walle Wallen <walle.sthlm@gmail.com> wrote:

Ah, I see.
Thank you for the enlightenment

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

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. :slight_smile:

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.

Yeah, good summary of all the drawbacks of this approach. :slight_smile:

Just do what Robert Klein said.

Erm, who?

Cheers

robert

···

2010/5/19 David Masover <ninja@slaphack.com>:

On Wednesday, May 19, 2010 09:54:03 am Robert Klemme wrote:

2010/5/19 Walle Wallen <walle.sthlm@gmail.com>:

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

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

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 Klemme wrote:

2010/5/19 David Masover <ninja@slaphack.com>:
> On Wednesday, May 19, 2010 09:54:03 am Robert Klemme wrote:
>> 2010/5/19 Walle Wallen <walle.sthlm@gmail.com>:

Unlike Robert Klein - Wikipedia who I can't recall
ever seeing around these parts, Robert Klemme CAN stop his leg, as far
as I know. <G>

···

On Wed, May 19, 2010 at 3:19 PM, David Masover <ninja@slaphack.com> wrote:

On Wednesday, May 19, 2010 01:53:58 pm Robert Klemme 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"?

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

> 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 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 :wink:

Cheers

robert

···

2010/5/19 Rick DeNatale <rick.denatale@gmail.com>:

On Wed, May 19, 2010 at 3:19 PM, David Masover <ninja@slaphack.com> wrote:

On Wednesday, May 19, 2010 01:53:58 pm Robert Klemme wrote:

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

Google Translate seems quite close to that, giving several possibilities:
http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme&lt;http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme&gt;
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)&lt;http://en.wikipedia.org/wiki/LEO_(computer)&gt;
http://www.leo-computers.org.uk
http://www.kzwp.com/lyons/index.htm <http://www.kzwp.com/lyons&gt;
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
Amazon.co.uk:Customer reviews: A COMPUTER CALLED LEO: Lyons Tea Shops and the world’s first office computer
... 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. ...

···

On Wed, May 19, 2010 at 9:48 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

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:
klemme - LEO: Übersetzung im ­Englisch ⇔ Deutsch Wörterbuch

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:
klemme - LEO: Übersetzung im ­Englisch ⇔ Deutsch Wörterbuch

Google Translate seems quite close to that, giving several possibilities:
http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme&lt;http://translate.google.co.uk/?&hl=en&lr=&tab=wT#de|en|klemme&gt;
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",

Hehe. :slight_smile: You know what? That's the typical phrase I use when
someone does not know how to spell my surname.

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)&lt;http://en.wikipedia.org/wiki/LEO_(computer)&gt;
http://www.leo-computers.org.uk
http://www.kzwp.com/lyons/index.htm <http://www.kzwp.com/lyons&gt;
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
Amazon.co.uk:Customer reviews: A COMPUTER CALLED LEO: Lyons Tea Shops and the world’s first office computer
... 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

···

2010/5/20 Colin Bartlett <colinb2r@googlemail.com>:

On Wed, May 19, 2010 at 9:48 PM, Robert Klemme <shortcutter@googlemail.com> > wrote:

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