Referencing an object through it's id string.

A

Alexandre Hudelot

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

Thanks in advance
 
E

Ezra Zygmuntowicz

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

Thanks in advance

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

David Vallner

Alexandre said:
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
 
K

Ken Bloom

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

David Vallner

Ken said:
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
 
A

Alexandre Hudelot

Thanks for the information!

David said:
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?
 
D

David Vallner

Alexandre said:
Thanks for the information!




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top