[email protected] wrote:
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.
I don’t know if it really rages.
I personally favor the idea of a so-called ordered hash,
but that’s just me. There’s no way to do it in current
Ruby (while still allowing hash literals). Before arguing
with me, read the previous parenthetical piece and understand
it.
I once talked with Matz about it, suggesting a new class (e.g.,
Map or AssociativeArray or whatever). I had assumed a new name
would be needed because a “hash” is inherently unordered, thus
interfering with the class name “Hash.” But Matz surprised me
by saying that if it were ordered, the name Hash could be kept,
as the underlying data structure was still a hash – just with
a little extra data preserved.
I’ve heard that Nobu made a patch for this, and I heard that
there were some questions about performance (not actual
measurements, just concerns). But I never heard anything beyond
that.
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.
It couldn’t break code as far as I can see.
I confess to being amused at one person who was so vehemently
against this that he said, “If Ruby added that, I would quit
using Ruby.” Truthfully I laughed out loud, because I thought
that was an extreme reaction.
But of course, he had a point in a way. People sometimes say, “If
you don’t like Feature X, then don’t use it.” But you still may
find yourself reading someone else’s code that does.
I’ll try to make a post soon about a use case for an ordered hash.
I’ve been meaning to collect a bunch of use cases, but haven’t
done so. Right now I can only think of one.
Hal