From: Daniel Finnie Date: 2006-12-11T04:39:41+09:00 Subject: Re: defining key equivalence in a hash If you put K.new into a variable, and then check for that, it will return true: irb(main):001:0> class K; def hash; 90; end; end => nil irb(main):002:0> h = {} => {} irb(main):003:0> k = K.new => # irb(main):004:0> h[k] = 1 => 1 irb(main):005:0> h[k] => 1 irb(main):006:0> h.key? k => true It's because K.new creates a new location in memory for each call to it, but h.key? checks the location in memory, not the contents of the keys and the objects passed to it. So if you call K.new twice, each will return a new location in memory. Dan Suraj Kurapati wrote: > Hello, > > I'm trying to make two keys equivalent by having their #hash method > produce the same result. But this does not seem to work: > > irb(main):001:0> class K; def hash; 90; end; end > => nil > irb(main):002:0> h = {} > => {} > irb(main):003:0> h[K.new] = 1 > => 1 > irb(main):004:0> h.key? K.new # <---- I want this to return true! > => false > > What am I missing? > > Thanks for your consideration. >