From: Kero Date: 2006-07-26T00:30:07+09:00 Subject: Re: Hash order bug? [snip] >>>> What's happening? all my code is behaving wrong because of that. >>> >>> Hashes are unordered. If you need an ordered collection, you'll need >>> to use an array. >> >> Oh my god, i didn't know it, sorry. Is that a missing feature? > > Debate rages on this point :-) It's mostly a speed issue, I think. > At least my memory is that Matz's latest statement was to the effect > that he would do it if it could be done efficiently. > > The question came up during a lunch here at OSCON (with me, Jim > Weirich, Pat Eyler, and a fellow named Nicolas whose last name I'm > afraid I don't remember) as to whether making hashes ordered in a > future Ruby would break any existing code. I think the answer is no, > which is kind of interesting considering what a major change it would > be. It's a case where the feature would be purely additive. I guess there won't be many ppl using algorithms that use the *un*sorted aspect of a Hash. Using Kernel#rand instead of Object#hash seems better. printing a few hash-values suggest they're not that random anyway... Other than that, would you sort the hash (or iterator) on its keys, its values or insertion sequence? NB: why did I read "addictive" at first pass? The question has been raised more often; and I admit I have overridden Hash#keys (and Hash each_pair, iirc) to achieve a (partially) sorted effect... The problem is that you can not generally sort keys (specifically not symbols).