A hash has no 'order' requirement. Items are added in an  
'implementation efficiency' order - which may be different on  
different underlying platforms.

If you want to retain a particular order, use an array.

Bob G

On Apr 4, 2006, at 07:43, Martin Boese wrote:

> Hello,
>
> I am building a menu structure for rails that I'd like to store in  
> a simple
> Hash.
>
> Now I found out that ruby Hashes do not keep the order, like this  
> program:
>
>> b = {'upkpgn'=>1,
>>      'jmay'=>2,
>>      'vkvxxm'=>3}
>>
>> b.each_key {|k|
>>  puts "%s => %s" % [k, b[k]]
>> }
>
> ...will output:
>
>> upkpgn => 1
>> vkvxxm => 3
>> jmay => 2
>
> (vkvxxm and jmay are swapped)
>
> I read an article describing this behavior, but it only mentions a  
> 'sort'
> solution which is useless for me because my menu has a logical order:
>
> http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/159776
>
> Are there any workarounds for this? Or shall I rather write my own  
> containers?
>
> BTW: Why is ruby doing this anyways...?!
>
> Thanks,
> Martin