From: Robert Klemme Date: 2003-12-05T20:02:04+09:00 Subject: Re: WeakRef and Object#hash "Samuel Tesla" schrieb im Newsbeitrag news:87zne8q76l.fsf@hiro.int.procacious.com... > I'm trying to implement a weak key hash to use for generic objects. > I'm more than happy to use Object#hash and Object#eql? as they come > with the distribution, but when I make the WeakRef, the hash values > don't come out the same way: > > irb(main):002:0> a = Object.new > => # > irb(main):003:0> a.hash > => 538332578 > irb(main):004:0> a = WeakRef.new(a) > => # > irb(main):005:0> a.hash > => 538323898 > > Is this by design, or is this a bug somewhere? I guess it's by design. But you can fix this easily by sub classing WeakRef class WeakHashRef < WeakRef def initialize(*args) super end def hash begin __getobj__.hash rescue WeakRef::RefError 0 end end def eql?(ref) begin __getobj__.id == ref.__getobj__.id rescue WeakRef::RefError false end end end Then you need to sub class Hash and override some methods if you want the WeakHash to look like a normal hash from the outside. Regards robert