"Hal Fulton" <hal9000 / hypermetrics.com> schrieb im Newsbeitrag
news:41AFA897.2080803 / hypermetrics.com...
> Robert Klemme wrote:
> > "itsme213" <itsme213 / hotmail.com> schrieb im Newsbeitrag
> > news:Mptrd.59198$KQ2.38410 / fe2.texas.rr.com...
> >
> >>Is there a pure-ruby ordered hash? I'm looking for something that will
> >>preserve the order in which key/value pairs were entered. The one on
RAA
> >
> > is
> >
> >>buggy http://raa.ruby-lang.org/project/orderedhash/
> >
> >
> > Whenever I hear "ordered hash" then I think of a hash whose keys are
> > ordered according to a definable order - not necessarily insertion
order.
> > In fact, insertion order seems to me a very special case.  Is this
really
> > widely used?
> >
>
> I'll reply to this, since an ordered hash is on my wish list.
>
> First of all, if it HAS an order, then it can be sorted (reordered)
> in any way you want. The (first) important thing to me is that it
> HAVE an order.

Definitely!

> Secondly, insertion order should IMO be the default -- because of
> hash literals, if for no other reason.

Hm, I'd prefer the "natural" order of key elements, i.e. the order defined
by <=>.  The reason being that you can get away with no additional data
stored.  If you make insertion oder the default order you're likely going
to have to wrap keys with some kind of other object that maintains an
insertion count (which then is used for ordering) and references the
original key.  If you base order on the key values only you can safe all
the hassle with maintaining insertion oder information, taking care of key
comparisons, hash values etc.

> If I say {this=>1, that=>2, other=>3} then that is what I expect
> (assuming there is any order at all). I would not want my hash
> literals scrambled (assuming some ordering, but one not based on
> insertion).

I can understand that from the point of view of source code, but in which
situations in applications does this really matter?

> But as long as a hash HAS an order, you can order it any way you
> like -- just as we can sort an array any way we like.

Yep, I'd do it similar to java.util.TreeMap - set a block as comparator
during hash creation that receives two values and returns -1,0 or 1 (same
as <=> does).

> And yes, I know there are fundamental differences between hashes
> and arrays. ;)

What differences?  Just kiddin'...  I'm in a strange mood today. :-)

Kind regards

    robert