# Referencing an object through it's id string

**URL:** <https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559>\
**Category:** Ruby\
**Created:** [August 27, 2006, 12:33am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559 "2006-08-27T00:33:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 12:33am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/1 "2006-08-27T00:33:19Z")

</div>

Hello, how does one set a pointer to foo using an id string obtained  
through foo.object\_id for instance?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 1:50am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/2 "2006-08-27T01:50:10Z")

</div>

Alexandre H. wrote:

> Hello, how does one set a pointer to foo using an id string obtained  
> through foo.object\_id for instance?

obj = ObjectSpace.id2ref object\_id

> Thanks in advance

See ri ObjectSpace from your command-line.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 4:56am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/3 "2006-08-27T04:56:41Z")

</div>

On Aug 26, 2006, at 7:38 PM, Alexandre H. wrote:

> Hello, how does one set a pointer to foo using an id string obtained  
> through foo.object\_id for instance?
> 
> Thanks in advance
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

## ---------------------------------------------------- ObjectSpace#\_id2ref ObjectSpace.\_id2ref(object\_id) → an\_object

```
  Converts an object id to a reference to the object. May not be
  called on an object id passed as a parameter to a finalizer.

     s = "I am a string" #=> "I am a string"
     r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
     r == s #=> true

```

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 5:16am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/4 "2006-08-27T05:16:54Z")

</div>

Alexandre H. wrote:

> Hello, how does one set a pointer to foo using an id string obtained  
> through foo.object\_id for instance?
> 
> Thanks in advance

Considering that object ids are only valid throughout one interpreter  
run… I’m curious: why pass around the object id instead of the object  
itself?

David V.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 6:33am UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/5 "2006-08-27T06:33:04Z")

</div>

On Sun, 27 Aug 2006 12:16:11 +0900, David V. wrote:

> Alexandre H. wrote:
> 
> > Hello, how does one set a pointer to foo using an id string obtained  
> > through foo.object\_id for instance?
> > 
> > Thanks in advance
> 
> Considering that object ids are only valid throughout one interpreter  
> run… I’m curious: why pass around the object id instead of the object  
> itself?

Have a look at how weakref.rb is implemented. It keeps track of the  
objects it’s referencing by object id, because the garbage collector  
can’t  
follow those, but the references can get their objects back  
using ObjectSpace.\_id2ref (assuming they haven’t been garbage  
collected).

–Ken B.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/6 "2006-08-27T12:56:16Z")

</div>

Ken B. wrote:

> > itself?
> 
> Have a look at how weakref.rb is implemented. It keeps track of the  
> objects it’s referencing by object id, because the garbage collector can’t  
> follow those, but the references can get their objects back  
> using ObjectSpace.\_id2ref (assuming they haven’t been garbage collected).

I figured as much, but that was the only behaviour I could think of that  
I’d implement using this. And since it’s in the standard library, I’d  
also probably use those weakrefs instead of rolling my own - I’m still  
living in the (naive?) idea that code that gets promoted into standard  
libraries works better than my implementations would.

But then I still wonder. Why pass around object ids instead of weakrefs  
or object references themselves?

David V.

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/7 "2006-08-27T13:54:41Z")

</div>

Thanks for the information!

David V. wrote:

> Alexandre H. wrote:
> 
> > Hello, how does one set a pointer to foo using an id string obtained  
> > through foo.object\_id for instance?
> > 
> > Thanks in advance
> 
> Considering that object ids are only valid throughout one interpreter  
> run… I’m curious: why pass around the object id instead of the object  
> itself?
> 
> David V.

I’m having trouble passing an object reference from a template (a form)  
to a controller method, what I’ve managed to return is the object’s id  
string.

I’m guessing there’s a better way of doing this?

---

<div class="post-metadata">

**Author:** ![orz42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@orz42](https://www.ruby-forum.com/u/orz42)\
**Post date:** [August 27, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/referencing-an-object-through-its-id-string/66559/8 "2006-08-27T15:02:32Z")

</div>

Alexandre H. wrote:

> > run… I’m curious: why pass around the object id instead of the object  
> > itself?
> > 
> > David V.
> 
> I’m having trouble passing an object reference from a template (a form)  
> to a controller method, what I’ve managed to return is the object’s id  
> string.

Is this a web application? (Rails maybe? Seeing as the words template  
and controller method cropped up.) It’s not quite best practice in that  
case since there are deployment models where this just doesn’t work -  
anything that involves several Ruby processes serving for instance. You  
also want to make sure the object doesn’t get garbage collected. But for  
simple cases, like a personal app, it’s pretty much the same thing as  
when you’d use a session I suppose.

David V.
