[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Referencing an object through it's id string.

Alexandre Hudelot

8/27/2006 2:39:00 AM

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

7 Answers

Ezra Zygmuntowicz

8/27/2006 2:56:00 AM

0


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

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

David Vallner

8/27/2006 3:16:00 AM

0

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

e

8/27/2006 3:56:00 AM

0

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

Ken Bloom

8/27/2006 4:27:00 AM

0

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?

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

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

David Vallner

8/27/2006 10:55:00 AM

0

Ken Bloom wrote:
> 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?
>
> 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

Alexandre Hudelot

8/27/2006 11:55:00 AM

0


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

David Vallner

8/27/2006 1:00:00 PM

0

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