From: Mark Hubbart Date: 2004-04-16T06:33:01+09:00 Subject: Re: Hash, ==, key-value comparison On Apr 15, 2004, at 10:04 AM, walter@mwsewall.com wrote: > Ok, > > ALthough the docs don't mention it, Hash comparison using == checks > not only the key value pairs, but also the default value / default > procs. > > So how does one compare 2 hashes key-value pairs. I can see that it > might be useful to distinguish between 2 hashes whose key-value pairs > are the same but the default value/default proc are different, but I > would think that would be much less often than wanting to know if the > key-value pairs are the same. I posted this in another thread, but I figured I should dup it here: for two hashes a and b: a.sort = b.sort It converts them to arrays, and sorts them. This should do it. > Maybe I am thinking about this all wrong, but I always viewed a Hash > as a Hash, and yes some might have a default value and some might > have a default proc, but it is the data (the key-value pairs) that, > to me, make up the Hash. The default data/procs are convenience that > make it easier to work with a Hash, but I would think that {} and > Hash.new(3) would be == if their key-value pairs would be equal. I agree that the way == works with hashes seems strange; I would expect it to just compare keys/values. OTOH, I can see how some might feel default values are an important part of determining equality. cheers, --Mark