Referencing an object through it's id string

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/.

---------------------------------------------------- ObjectSpace#_id2ref
      ObjectSpace._id2ref(object_id) -> an_object

···

On Aug 26, 2006, at 7:38 PM, Alexandre Hudelot 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/\.

------------------------------------------------------------------------
      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

Alexandre Hudelot 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 Vallner

Alexandre Hudelot 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.

···

--
Posted via http://www.ruby-forum.com/\.

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 Bloom

···

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

Alexandre Hudelot 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?

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Thanks for the information!

David Vallner wrote:

Alexandre Hudelot 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 Vallner

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?

···

--
Posted via http://www.ruby-forum.com/\.

Ken Bloom wrote:

Alexandre Hudelot 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).

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 Vallner

···

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

Alexandre Hudelot wrote:

Thanks for the information!

David Vallner wrote:

Alexandre Hudelot 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 Vallner

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 Vallner