ordered hashes in ruby 1.9 19. Jun 2008
If you create a hash in Ruby 1.8 and then browse it with the each-iterator you get the keys delivered in abitrary order.
>> hash = {:a=>1, :b=>2, :c=>3,}
>> hash.each {|key,value| puts "#{key}=>#{value}"}
c=>3
b=>2
a=>1
This might be okay for some cases, but in other cases you might want the insertation order preserved. Well, good news in Ruby 1.9 this works.
>> hash = {:a=>1, :b=>2, :c=>3,}
>> hash.each {|key,value| puts "#{key}=>#{value}"}
a=>1
b=>2
c=>3
Kommentare (1)