From: Eric Hodel Date: 2004-04-07T13:58:28-07:00 Subject: Re: IdentityHash, like Hash but with keys using equals? vs eql? --Zqkt5x/gGOIVPcL0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Jean-Hugues ROBERT (jean_hugues_robert@yahoo.com) wrote: > I just encountered a rare case where I need a Hash where keys > are object id. Class Hash compares keys by value. As a result > two different String with the same value are considered the > same key. This is fine in most cases. But right now I need to > be more restrictive and I need two String objects to be treated > as two keys even if they have the same value. Not entirely true. Hash uses #hash and #eql? to decide how to store entries. Some objects (String) implement #hash, but by default Ruby uses #object_id for #hash: irb(main):001:0> f =3D "foo" irb(main):002:0> f.hash =3D> 876516207 irb(main):003:0> f.id =3D> 67718468 irb(main):004:0> o =3D Object.new =3D> # irb(main):005:0> o.hash =3D> 67714018 irb(main):006:0> o.id =3D> 67714018 > I think IdentityHash is a probable name for such a beast, yet > I could not find one in Ruby and I don't see any method in > class Hash that I could redefine in a derived class to change > the way keys are compared. How about instead extending Strings you want stored uniquely to return #object_id for #hash? --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Zqkt5x/gGOIVPcL0 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFAdGt0MypVHHlsnwQRAl6IAKC7R8k2lhNialRV9sDVeKj4nbsf3gCg9xiI uoCD7qWOxjd0VtLwuQo/y1g= =WSNT -----END PGP SIGNATURE----- --Zqkt5x/gGOIVPcL0--