I think it boils down to: there is no Hash#hash which inspects the
contents - Hash inherits Object#hash which is Object#object_id, and
the two hashes, while they appear to have the same contents, are not
the same object. So the keys are not “identical”.
will return Array, instead of Symbol, which is what you’re probably
expecting. So, the hash doesn’t have identical keys. It has two
different keys, each of which is a different array with identical
members. Try
p POOLS[self].keys[0].object_id
p POOLS[self].keys[1].object_id
I see what your saying. I was using #== not #eql? in comparing the
keys. So I see why it’s faling now. How do I get aorund this? I’m
caching object based on therr initialization paramaters, which has to
be an array. Is there a simpler way or do I have to do something like:
I see what your saying. I was using #== not #eql? in comparing the
keys. So I see why it’s faling now. How do I get aorund this? I’m
caching object based on therr initialization paramaters, which has to
be an array. Is there a simpler way or do I have to do something like:
class Parameters < Array
alias :eql? :==
end
Ugh. Nothing like that works either. It’s not using eql? or equal?, but
rather #hash (I guess that’s actually what you were trying to say Ara).
This doen’t make any sense to me. Why do identical strings and arrays
have the same #hash value but not hashes?
Ugh. Nothing like that works either. It’s not using eql? or equal?, but
rather #hash (I guess that’s actually what you were trying to say Ara).
This doen’t make any sense to me. Why do identical strings and arrays
have the same #hash value but not hashes?
Nope. It doesn’t even use #hash. So it must be using #object_id with an
exception for Strings and Arrays. Dissapointing to say the least.
I had to resort to recursively converting all hashes to arrays.
Ugh. Nothing like that works either. It’s not using eql? or equal?,
but
rather #hash (I guess that’s actually what you were trying to say
Ara).
A Hash only asks if one object is #eql? to another when they have the
same #hash. You can’t use a Hash as a Hash key because Hash#hash is
not implemented that way.
This doen’t make any sense to me. Why do identical strings and arrays
have the same #hash value but not hashes?
in otherwords, contructing two objects with the same argument lists will
contruct only one object. it’s the argument lists which are used to
determine
uniqueness. alternatively uniqueness will be determined via the method
‘multiton_id’ if you’re class has implimented that instance method or
the
object in question responds to it otherwise. so, in your case, you
might use
something like
hash.to_a.sort.hash
or something unique like that. eg
class MyClass
include Multiton
attr :multiton_id
def initialize h = {}
@multiton_id = h.to_a.sort.hash
super
end
Nice. Doesn’t matter a whole lot a few keys but there is a clear slow
down.
I came up with another possibility however. I won;t work for all cases,
but using Marshal.dup on the args instead of converting to assoc gives
the proper result too. Wonder how that would benchmark?