Hello, how does one set a pointer to foo using an id string obtained
through foo.object_id for instance?
Thanks in advance
Hello, how does one set a pointer to foo using an id string obtained
through foo.object_id for instance?
Thanks in advance
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.
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/.
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 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.
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.
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.
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?
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs