I’ve seen several posts related in some way to the subject of using
hashes as keys in hashes, but I haven’t seen a clear solution to the
issue. My problem is I want to generate a set of unique hashes.
Since hash keys are unique, I was hoping to just put the hashes in as
keys, something like:
myUniqueHashes[aHash] ||= 0 myUniqueHashes[aHash] += 1
This would not only give me a list of unique hashes, but it would
also tell me how many times each one was seen.
Unfortunately, this does not work because in a hash, each different
hash that is inserted as a key is considered to be different, even if
the contents are the same. When used as keys in a hash, two hashes
are considered equal if aHash.id = bHash.id, meaning if they are the
very same hash located at the same place in memory.
At the micro level, what I need is a special kind of hash that will
consider two hash keys to be equal if aHash == bHash. At a higher
level, I need an efficient way to collect a unique set of hashes. An
array would work, but for a large set it’d be much slower…and
storing the number of accesses would be relatively clunky compared to
a hash’s interface.
What is the Ruby way to solve this problem?