# Look up objects by object\_id

**URL:** <https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987>\
**Category:** Ruby\
**Created:** [November 10, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987 "2008-11-10T22:11:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 10, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987/1 "2008-11-10T22:11:04Z")

</div>

So ruby objects have an object\_id. What is the intended purpose of  
these unique identifiers? Is their a method that allows one to  
retrieve a object by object\_id? Thanks in advanced.

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 10, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987/2 "2008-11-10T22:30:30Z")

</div>

On 10.11.2008 22:06, exiquio wrote:

> So ruby objects have an object\_id. What is the intended purpose of  
> these unique identifiers? Is their a method that allows one to  
> retrieve a object by object\_id? Thanks in advanced.

[http://ruby-doc.org/core/classes/ObjectSpace.html#M006793](http://ruby-doc.org/core/classes/ObjectSpace.html#M006793)

robert

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 10, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987/3 "2008-11-10T22:32:57Z")

</div>

exiquio wrote:

> So ruby objects have an object\_id. What is the intended purpose of  
> these unique identifiers? Is their a method that allows one to  
> retrieve a object by object\_id? Thanks in advanced.

The object\_id is an encoded version of the object reference and class.  
For certain classes (e.g. Fixnum) you can decode the actual value. For  
other classes it encodes the memory address where the object data  
structure can be found.

If two object references have the same object\_id, then they are the same  
object.

As for retrieving by object\_id, see ObjectSpace.\_id2ref(ref). There are  
practical examples in drb/drb.rb and weakref.rb in the standard library  
that you can look at.

---

<div class="post-metadata">

**Author:** ![exiquio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@exiquio](https://www.ruby-forum.com/u/exiquio)\
**Post date:** [November 10, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/look-up-objects-by-object-id/152987/4 "2008-11-10T23:00:37Z")

</div>

On Nov 10, 4:30 pm, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If two object references have the same object\_id, then they are the same  
> object.
> 
> ## As for retrieving by object\_id, see ObjectSpace.\_id2ref(ref). There are practical examples in drb/drb.rb and weakref.rb in the standard library that you can look at.
> 
> Posted viahttp://www.ruby-forum.com/.

thanks for the help
