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.
Walt
···
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
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.
–
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================
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.
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
I can contribute a patch to:
Change ‘==’ method to compare only key-value pairs.
Add Hash#=== method to act like current Hash#==
Comments?
Personally, I like the idea. You get my vote!
Regards,
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
> >
> > 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?
>
Thanks, but that doesn’t work
a = {‘A’=>1, ‘B’=>2, ‘C’=>3}
b = {‘A’=>3, ‘B’=>2, ‘C’=>1}
puts (a.keys - b.keys).empty? and (a.values - b.values).empty?
here the keys and values are the same but belong to different
elements.
I know ways to do it, but it seems ugly and overcomplicated. Also
since it is pure ruby it is much
c = Hash.new{|h,k| h[k]=0}
c[‘A’] = 1
c[‘B’] = 2
c[‘C’] = 3
I am sure I can get a much faster ruby version, but not as nice as a
== b.
Walt
···
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
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?
Does not ensure that key value mappings are identical:
irb(main):006:0> a={1=>1,2=>2}
=> {1=>1, 2=>2}
irb(main):007:0> b={1=>2,2=>1}
=> {1=>2, 2=>1}
irb(main):008:0> (a.keys - b.keys).empty? and (a.values - b.values).empty?
=> true
irb(main):009:0> a == b
=> false
In this case == is more correct than you suggested comparison.
On Fri, Apr 16, 2004 at 02:04:10AM +0900, 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
I can contribute a patch to:
Change ‘==’ method to compare only key-value pairs.
Add Hash#=== method to act like current Hash#==
Comments?
Personally, I like the idea. You get my vote!
–
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
Change ‘==’ method to compare only key-value pairs.
Add Hash#=== method to act like current Hash#==
They are possible options, along with
add new method (e.g. content_equal?) for membership equality.
I’m not sure which one is the best way to go.
matz.
I think that most common case for == would be content equals and not
content and default value/proc equal.
Nobu has a good point that === would make more sense to be a
membership test, i.e., alias for Hash#key?.
Perhaps eql? would could be content equal and default value/block.
This would be similar Numeric where 1 == 1.0 is true but 1.eql?(1.0)
is false.
Thanks for listening,
Walt
···
In message “Re: Hash, ==, key-value comparison” > on 04/04/16, Elias Athanasopoulos elathan@phys.uoa.gr writes:
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
I tend to think about == as “has the same value as”.
So == is rather strong.
I like it this way, so I would rather stick with the exiting scheme.
OTOH I agree that the default value/proc is easy to miss. Yet,
maybe some other operator could do the job, something like ~= if available ?
Jean-Hugues
···
At 02:30 16/04/2004 +0900, you wrote:
On Fri, Apr 16, 2004 at 02:04:10AM +0900, 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
I can contribute a patch to:
Change ‘==’ method to compare only key-value pairs.
Add Hash#=== method to act like current Hash#==
Comments?
Personally, I like the idea. You get my vote!
Regards,
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
Thanks,
Walt
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
I feel Hash#=== would test membership, i.e., alias for
Hash#key?.
–
Nobu Nakada
I’d agree with that. This way you could easily test for many possible
values in a case statement.
Note that if there are many vaes to match with, then a hash may be
more efficent then a “when” with a comma-delimited list, as long as the
hash is calculated only once. (Of course, if there are multiple “when”
branches like that, a single hash that does all the matching or probably a
hash of proces or lambdas (I don’t quite know the fine point of
distinguishing these) is better.)
Let me mention how this will work in perl6. In perl6, the =~ operator will
do something similar to ruby’s ===, that is, it is used in the when clauses,
it matches numeric or string data, regexps, and classes just like === in
ruby (and thus does not automatically coerce a string to a regexp as =~ in
per5 or ruby). When it meets a hash or an array on either side, it mathces
each key (element of array) with the other side until it finds a match.
To do this with an array is quite unneccesarry I think, but it is good for a
haash.
“Zsban Ambrus” ambrus@math.bme.hu schrieb im Newsbeitrag
news:20040417194736.GA1309@math.bme.hu…
Hi,
…
I feel Hash#=== would test membership, i.e., alias for
Hash#key?.
–
Nobu Nakada
I’d agree with that. This way you could easily test for many possible
values in a case statement.
Does this work? I mean
h = {}
case h
when val1, val2
…
end
Does something else: it tests val1 === h and val2 === h and not h === val1.
Currently I don’t see how Hash#=== can be used in a case to test for
multiple values. In order to use Hash#=== in a case statement, the hash has
to appear in the when clauses. Do I overlook something?
bad= {}; # bad magic words that the player might try
%w[abracadabra xyzzy hocuspocus bringmehome].each do |x| bad=1 end;
this list of (bad) magic words might get very long
…
(in a loop)
…
gets
~/^\s*(\w+)/ and word= $1
case word
when bad
puts “That won’t work in this game. Nice try, thuogh.”
when “aardvark” # this is the real magic word
player.move(Home)
when “a”, “ahead”
player.move_ahead
when “fight”, “shoot”
(… check if there’s a monster here, kill it with 1/2 probability,
otherwise make it angry …)
…
end
···
On Sun, Apr 18, 2004 at 07:39:11PM +0900, Robert Klemme wrote:
“Zsban Ambrus” ambrus@math.bme.hu schrieb im Newsbeitrag
news:20040417194736.GA1309@math.bme.hu…
I feel Hash#=== would test membership, i.e., alias for
Hash#key?.
–
Nobu Nakada
I’d agree with that. This way you could easily test for many possible
values in a case statement.
Does this work? I mean
h = {}
case h
when val1, val2
…
end
Does something else: it tests val1 === h and val2 === h and not h === val1.
Currently I don’t see how Hash#=== can be used in a case to test for
multiple values. In order to use Hash#=== in a case statement, the hash has
to appear in the when clauses. Do I overlook something?
“Zsban Ambrus” ambrus@math.bme.hu schrieb im Newsbeitrag
news:20040418110453.GA2123@math.bme.hu…
“Zsban Ambrus” ambrus@math.bme.hu schrieb im Newsbeitrag
news:20040417194736.GA1309@math.bme.hu…
Hi,
…
I feel Hash#=== would test membership, i.e., alias for
Hash#key?.
–
Nobu Nakada
I’d agree with that. This way you could easily test for many
possible
values in a case statement.
Does this work? I mean
h = {}
case h
when val1, val2
…
end
Does something else: it tests val1 === h and val2 === h and not h ===
val1.
Currently I don’t see how Hash#=== can be used in a case to test for
multiple values. In order to use Hash#=== in a case statement, the
hash has
to appear in the when clauses. Do I overlook something?
Regards
robert
No, I mean something like this:
bad= {}; # bad magic words that the player might try
%w[abracadabra xyzzy hocuspocus bringmehome].each do |x| bad=1 end;
this list of (bad) magic words might get very long
…
(in a loop)
…
gets
~/^\s*(\w+)/ and word= $1
case word
when bad
puts “That won’t work in this game. Nice try, thuogh.”
when “aardvark” # this is the real magic word
player.move(Home)
when “a”, “ahead”
player.move_ahead
when “fight”, “shoot”
(… check if there’s a monster here, kill it with 1/2 probability,
otherwise make it angry …)
…
end
Ah, ok I see. Thx! I like especially the “when bad”.
robert
···
On Sun, Apr 18, 2004 at 07:39:11PM +0900, Robert Klemme wrote: