From: Phrogz Date: 2007-02-09T08:05:06+09:00 Subject: Re: Ruby hash equlity 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.