From: "Ara.T.Howard" Date: 2004-04-16T03:04:10+09:00 Subject: Re: Hash, ==, key-value comparison On Fri, 16 Apr 2004 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. > > 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. > > Assuming that I am in the minority here about ==, what is the proper > way to see if 2 Hashes key-value pairs are the same. how about (a.keys - b.keys).empty? and (a.values - b.values).empty? -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================