From: mike.leddy@... Date: 2007-02-09T22:25:12+09:00 Subject: Re: Ruby hash equlity Thanks Phrogz, but yes my problem would require nested hashes and would have to be reasonably efficient. Let me explain..... I started with the problem of comparing sets that may have sets as members: irb(main):017:0> require 'set' => false irb(main):018:0> Set[:a, :b] == Set[:a, :b] => true irb(main):019:0> Set[:a, :b, Set[:c, :d]] == Set[:a, :b, Set[:c, :d]] => false Suprisingly it didn't work. So I looked at how sets were implemented - basically hashes where the set members are the keys and the values are true. I still believe that hash equality should be as the 'ri' manual describes. I'm going to examine the ruby 'C' code to see exactly why..... Mike On Feb 8, 8:03 pm, "Phrogz" wrote: > Finally, here's a slightly better version. It's still O(n^2) in the > worst case, but should perform much better under common circumstances > (in case someone actually needed this functionality): > > class Hash > # Ouch! O(n^2) performance > # Bad for hashes with many keys > def sort_of_equal( other ) > equal = true > self.each{ |k,v| > unless found_key = (other[k]==v) > other.each{ |k2,v2| > break if found_key = ( ( k==k2 ) && ( v==v2 ) ) > } > end > equal &&= found_key > } > equal > end > end > > Note that this will still not work if you have nested hashes that you > want to treat like this. You'd need to override Hash#== fully, or put > in tests based on key and value type.