[Nuby] Object ID Lookup?

J

Jim Moy

Hi all, a Ruby Nuby here with a question.

Is there a way to lookup an object reference from the object's id?
Something to do like the following, but without the need for the lookup
hash:

a = { "a" => 1, "b" => 2 }
b = { "c" => 4, "d" => 3 }
arr = [a, b]
ids = arr.collect {|h| h.id}
lookup = {}
arr.each { |h| lookup[h.id] = h }
cmp = ids.collect {|id| lookup[id]}
ids == cmp => true

In my instance, I've got a reference set of hashes that I'm pointing to
from multiple arrays. Those arrays are manipulated depending on sorting
and other factors.

I don't want to get Hash#== involved so I'm using the Hash object id's.
After processing is completed, I'd like to get back from the IDs to the
object references. The lookup is working, but I was wondering if there
was a better method.

Thanks for any suggestions,

Jim Moy
Fort Collins, Colorado
USA
 
J

Jim Moy

Jim said:
You can use ObjectSpace._id2ref(x.id) to get a reference to x.

Thanks for the answer. My Ruby in a Nutshell says this is an "internal
use only" method, is that still the case as of 1.8?

Jim
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: [Nuby] Object ID Lookup?"

|> You can use ObjectSpace._id2ref(x.id) to get a reference to x.
|
|Thanks for the answer. My Ruby in a Nutshell says this is an "internal
|use only" method, is that still the case as of 1.8?

I don't recommend you to use it too often, because it's difficult to
use it right. The referencing object may be recycled by GC.

matz.
 
E

Eric Hodel

--bmlge7Pg4VPvffji
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hi all, a Ruby Nuby here with a question.
=20
Is there a way to lookup an object reference from the object's id?=20
Something to do like the following, but without the need for the lookup
hash:
=20
a =3D { "a" =3D> 1, "b" =3D> 2 }
b =3D { "c" =3D> 4, "d" =3D> 3 }
arr =3D [a, b]
ids =3D arr.collect {|h| h.id}
lookup =3D {}
arr.each { |h| lookup[h.id] =3D h }
cmp =3D ids.collect {|id| lookup[id]}
ids =3D=3D cmp =3D> true
=20
In my instance, I've got a reference set of hashes that I'm pointing to
from multiple arrays. Those arrays are manipulated depending on sorting
and other factors.
=20
I don't want to get Hash#=3D=3D involved so I'm using the Hash object id'= s.=20
After processing is completed, I'd like to get back from the IDs to the
object references. The lookup is working, but I was wondering if there
was a better method.

Not seeing the internals of your code, will Object#equal? (identical
object ids) be able to replace #=3D=3D?

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--bmlge7Pg4VPvffji
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQE/3j/ZMypVHHlsnwQRAqq6AJ0UvivlN563tHun7GtAvVRVydxsjACfZo40
bv58DcgrBN00bjUjX/vURvk=
=mm3r
-----END PGP SIGNATURE-----

--bmlge7Pg4VPvffji--
 
J

Jim Moy

a = { "a" => 1, "b" => 2 }
b = { "c" => 4, "d" => 3 }
arr = [a, b]
ids = arr.collect {|h| h.id}
lookup = {}
arr.each { |h| lookup[h.id] = h }
cmp = ids.collect {|id| lookup[id]}
ids == cmp => true

In my instance, I've got a reference set of hashes that I'm
pointing to from multiple arrays. Those arrays are manipulated
depending on sorting and other factors.

I don't want to get Hash#== involved so I'm using the Hash
object id's. After processing is completed, I'd like to get
back from the IDs to the object references. The lookup is
working, but I was wondering if there was a better method.

Eric Hodel ([email protected]) wrote:

Not seeing the internals of your code, will Object#equal? (identical
object ids) be able to replace #==?

Yes, the code similar to the above example works as-is with the
hash ids, as I wanted to avoid Hash#== and all the comparisons it
will perform. Judging by the replies so far, I think I'll keep
the lookup hash.

Jim
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top