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?

If all you're using it for is to test existence of keys, then Hash can 
be replaced with SortedSet. See 'ri Set' for more info (the two classes 
are in the same file in the standard distribution). Otherwise, you will 
need to find or implement a Hash that maintains key order.

--
Toby DiPasquale

-- 
Posted via http://www.ruby-forum.com/.