From: Rick DeNatale Date: 2007-03-03T07:37:50+09:00 Subject: Re: Hash keys don't work as expected On 3/2/07, Robert Dober wrote: > On 3/2/07, Robert Klemme wrote: > > More precisely if the hash of the key changes the bucket changes. And > > since Array calculates it's hash based on the content changing the > > content means changing the key. And, to the OP, lookups in Hashes are > > not done via #object_id but via #hash: > > http://en.wikipedia.org/wiki/Hashtable > which all is necessary in order to have mutable keys, I guess that it > is very difficult to think about that in the beginning when not yet > used to it. Actually it doesn't spring directly from supporting mutable keys, but from wanting to have the keys match based on equality rather than identity. If Hash used identity instead of hash then a = "key".freeze # make the key immutable h = {a => 1} h["key"] ==> nil even though nothing got mutated. > BTW. I try to avoid to mutate hash keys, I know my limits ;) Yes if you really want to be safe you can always clone and freeze keys when inserting into a hash. But that's probably overkill. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/