[Nuby] Object ID Lookup?

Hi all, a Ruby Nuby here with a question.

Is there a way to lookup an object reference from the object’s id?
Something to do like the following, but without the need for the lookup
hash:

a = { “a” => 1, “b” => 2 }
b = { “c” => 4, “d” => 3 }
arr = [a, b]
ids = arr.collect {|h| h.id}
lookup = {}
arr.each { |h| lookup[h.id] = h }
cmp = ids.collect {|id| lookup[id]}
ids == cmp => true

In my instance, I’ve got a reference set of hashes that I’m pointing to
from multiple arrays. Those arrays are manipulated depending on sorting
and other factors.

I don’t want to get Hash#== involved so I’m using the Hash object id’s.
After processing is completed, I’d like to get back from the IDs to the
object references. The lookup is working, but I was wondering if there
was a better method.

Thanks for any suggestions,

Jim Moy
Fort Collins, Colorado
USA

Jim Moy wrote:

Is there a way to lookup an object reference from the object’s id?

You can use ObjectSpace._id2ref(x.id) to get a reference to x.

Not seeing the internals of your code, will Object#equal? (identical
object ids) be able to replace #==?

···

Jim Moy (web@jimmoy.com) wrote:

Hi all, a Ruby Nuby here with a question.

Is there a way to lookup an object reference from the object’s id?
Something to do like the following, but without the need for the lookup
hash:

a = { “a” => 1, “b” => 2 }
b = { “c” => 4, “d” => 3 }
arr = [a, b]
ids = arr.collect {|h| h.id}
lookup = {}
arr.each { |h| lookup[h.id] = h }
cmp = ids.collect {|id| lookup[id]}
ids == cmp => true

In my instance, I’ve got a reference set of hashes that I’m pointing to
from multiple arrays. Those arrays are manipulated depending on sorting
and other factors.

I don’t want to get Hash#== involved so I’m using the Hash object id’s.
After processing is completed, I’d like to get back from the IDs to the
object references. The lookup is working, but I was wondering if there
was a better method.


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Jim Moy wrote:

Is there a way to lookup an object reference from the object’s id?

You can use ObjectSpace._id2ref(x.id) to get a reference to x.

Thanks for the answer. My Ruby in a Nutshell says this is an “internal
use only” method, is that still the case as of 1.8?

Jim

a = { “a” => 1, “b” => 2 }
b = { “c” => 4, “d” => 3 }
arr = [a, b]
ids = arr.collect {|h| h.id}
lookup = {}
arr.each { |h| lookup[h.id] = h }
cmp = ids.collect {|id| lookup[id]}
ids == cmp => true

In my instance, I’ve got a reference set of hashes that I’m
pointing to from multiple arrays. Those arrays are manipulated
depending on sorting and other factors.

I don’t want to get Hash#== involved so I’m using the Hash
object id’s. After processing is completed, I’d like to get
back from the IDs to the object references. The lookup is
working, but I was wondering if there was a better method.

Not seeing the internals of your code, will Object#equal? (identical
object ids) be able to replace #==?

Yes, the code similar to the above example works as-is with the
hash ids, as I wanted to avoid Hash#== and all the comparisons it
will perform. Judging by the replies so far, I think I’ll keep
the lookup hash.

Jim

···

Eric Hodel (drbrain@segment7.net) wrote:

Hi,

···

In message “Re: [Nuby] Object ID Lookup?” on 03/12/13, “Jim Moy” web@jimmoy.com writes:

You can use ObjectSpace._id2ref(x.id) to get a reference to x.

Thanks for the answer. My Ruby in a Nutshell says this is an “internal
use only” method, is that still the case as of 1.8?

I don’t recommend you to use it too often, because it’s difficult to
use it right. The referencing object may be recycled by GC.

						matz.