See the example below
require "set"
s = [[1, 2], [3, 4]].to_set # s = {[1, 2], [3, 4]}
m = s.max_by {|a| a[0]} # m = [3, 4]
m[0] = 9 # m = [9, 4], s = {[1, 2], [9, 4]}
s.delete(m) # s = {[1, 2], [9, 4]} ?????
This behaves differently from an array. (If we remove .to_set
, we will get s = [[1, 2]]
which is expected.) Is this a bug?
Yes, this is a bug or at least I'd call it a bug. Some would call this "an implementation detail accidentally leaking to the outside world" but that's just fancy pants city-boy talk for bug.
The problem has two main causes:
The result is that you're modifying the internal Hash's keys without the Hash knowing about it and that confuses the poor Hash into not really knowing what keys it has anymore. The Hash class has a
rehash
method:Notice the interesting behavior in the example included with the
rehash
documentation. Hashes keep track of things using thek.hash
values for the keyk
. If you have an array as a key and you change the array, you can change the array'shash
value as well; the result is that the Hash still has that array as a key but the Hash won't be able to find that array as a key because it will be looking in the bucket for the newhash
value but the array will be in the bucket for the oldhash
value. But, if yourehash
the Hash, it will all of a sudden be able to find all of its keys again and the senility goes away. Similar problems will occur with non-array keys: you just have to change the key in such a way that itshash
value changes and the Hash containing that key will get confused and wander around lost until yourehash
it.The Set class uses a Hash internally to store its members and the members are used as the hash's keys. So, if you change a member, the Set will get confused. If Set had a
rehash
method then you could kludge around the problem by slapping the Set upside the head withrehash
to knock some sense into it; alas, there is no such method in Set. However, you can monkey patch your own in:Then you can change the keys, call
rehash
on the Set, and yourdelete
(and various other methods such asmember?
) will work properly.