Robert Klemme
2/18/2008 12:40:00 PM
2008/2/18, Pokkai Dokkai <bad_good_lion@yahoo.com>:
> suppose a object (obj1) have another object (obj2) itself.
> how to find obj1.object_id inside obj2 ?
You can't. Well, you can try but there is no guarantee that you will
find all obj1 that reference obj1 because it won't work for Structs:
irb(main):001:0> S=Struct.new :obj
=> S
irb(main):002:0> s1 = S.new
=> #<struct S obj=nil>
irb(main):003:0> s2 = S.new s1
=> #<struct S obj=#<struct S obj=nil>>
irb(main):004:0> ObjectSpace.each_object(Object) do |obj|
irb(main):005:1* puts obj.object_id if
irb(main):006:1* obj.instance_variables.any? {|iv|
obj.instance_variable_get(iv).equal? s1}
irb(main):007:1> end
=> 4228
irb(main):008:0> s2.object_id
=> 1073522830
irb(main):009:0> s2.instance_variables
=> []
irb(main):010:0>
If obj2 needs to know, you should explicitly tell it.
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end