
Submitted by Eludias (Thu Aug 17 19:57:27 UTC 2006)
For example:
a = {}
a[[1,2]] = 10
..now a[[1,2]] gives 10.
a[{1=>2}] = 11
..now a[{1=>2}] gives nil.
I stumbled upon this while grouping an array of objects based on an aspect (something like Array#sort_by). When I changed the aspect from an Array to an Hash, the grouping stopped working.
# By default, the hashcode of hash doesn't look at the
# contents. This makes indexing a hash with a hash impossible.
def hash
inject(0) { |h, p| h ^ p[0].hash ^ p[1].hash }
end
# When implementing #hash, eql? should be adapted.
def eql?(other)
super || other == self
end
end

| Comments | Current voting | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
I voted mildly against it, mostly because I personally think that Hash#hash is not a really good name, if you compare it to other methods like Hash#keys for example.
|
|


RCRchive copyright © David Alan Black, 2003-2005.
Powered by .