# \[Nuby\] Object ID Lookup?

**URL:** <https://rubytalk.org/t/nuby-object-id-lookup/9308>\
**Category:** ruby-talk\
**Created:** [12 December 2003 22:05 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308 "2003-12-12T22:05:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Moy](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jim\_Moy](https://rubytalk.org/u/Jim_Moy)\
**Post date:** [12 December 2003 22:05 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/1 "2003-12-12T22:05:58Z")

</div>

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](http://h.id)}  
lookup = {}  
arr.each { |h| lookup[[h.id](http://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

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [12 December 2003 22:11 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/2 "2003-12-12T22:11:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [15 December 2003 23:12 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/3 "2003-12-15T23:12:28Z")

</div>

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

> **···**
>
> Jim Moy ([web@jimmoy.com](mailto: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](mailto:drbrain@segment7.net) - [http://segment7.net](http://segment7.net)  
> All messages signed with fingerprint:  
> FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

---

<div class="post-metadata">

**Author:** ![Jim\_Moy](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jim\_Moy](https://rubytalk.org/u/Jim_Moy)\
**Post date:** [13 December 2003 00:48 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/4 "2003-12-13T00:48:59Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Jim\_Moy](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jim\_Moy](https://rubytalk.org/u/Jim_Moy)\
**Post date:** [16 December 2003 01:29 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/5 "2003-12-16T01:29:39Z")

</div>

> > 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](mailto:drbrain@segment7.net)) wrote:

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [13 December 2003 02:26 UTC](https://rubytalk.org/t/nuby-object-id-lookup/9308/6 "2003-12-13T02:26:21Z")

</div>

Hi,

> **···**
>
> In message “Re: [Nuby] Object ID Lookup?” on 03/12/13, “Jim Moy” [web@jimmoy.com](mailto: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.
> 
> ```
