# Re: Hash order bug?

**URL:** <https://www.ruby-forum.com/t/re-hash-order-bug/99796>\
**Category:** Ruby\
**Created:** [July 25, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/re-hash-order-bug/99796 "2006-07-25T16:15:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 25, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/re-hash-order-bug/99796/1 "2006-07-25T16:15:14Z")

</div>

> > > =\> false}  
> > > Now, i delete the foo function from the code (the foo
> > 
> > Hashes are unordered. If you need an ordered collection,  
> > you’ll need  
> > to use an array.
> > 
> > David
> 
> Oh my god, i didn’t know it, sorry. Is that a missing feature?

No, it’s just the way hashes behave. Note that there are ordered hash  
implementations on the RAA:

[http://raa.ruby-lang.org/project/orderedhash/](http://raa.ruby-lang.org/project/orderedhash/)

You can also use a Struct to get roughly the same effect.

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 26, 2006, 2:39am UTC](https://www.ruby-forum.com/t/re-hash-order-bug/99796/2 "2006-07-26T02:39:29Z")

</div>

Berger, Daniel wrote:

> No, it’s just the way hashes behave. Note that there are ordered hash  
> implementations on the RAA:
> 
> [http://raa.ruby-lang.org/project/orderedhash/](http://raa.ruby-lang.org/project/orderedhash/)
> 
> You can also use a Struct to get roughly the same effect.

In case anyone tries to refer me to these, I’ll point out that  
there are _three_ reasons I like a hash.

1. It indexes on an arbitrary value, not on an integer like  
an array.
2. It consists of explicit pairings of key and value.
3. There is a convenient sybtax for literals:  
{a=\>b, c=\>d, e=\>f}

Note that there are _no_ ordered hash solutions that preserve  
the third criterion.

Most would require me to do something like:

oh = OrdHash.new(a,b, c,d, e,f)

or

oh = OrdHash.new([a,b], [c,d], [e,f])

or something just as bad.

Don’t underestimate the importance of a convenient notation!  
What if arrays in Ruby were like arrays in C? In C, you can  
specify an array literal (barely) when you declare it; that’s  
about it.

Hal
