From: Jean-Hugues ROBERT Date: 2004-04-08T06:38:25+09:00 Subject: Re: IdentityHash, like Hash but with keys using equals? vs eql? At 05:58 08/04/2004 +0900, you wrote: >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: Thanks for the clarification. > > 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? Would be OK if my IdentityHash were to contain strings only. But I need to put arbitrary objects in it. BTW: I need that to detect "true" circular paths when walking a tree/graph. Rigth now I detect false circulars when nodes' values compare equal where in fact it is two different node objects (with the same "value"). Thanks. Jean-Hugues >-- >Eric Hodel - drbrain@segment7.net - http://segment7.net >All messages signed with fingerprint: >FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17