Ruby on Rails Monday, February 24, 2014


Hashes are by definition unordered. If you think you need an ordered hash, then you probably don't understand hashes...

Ruby 1.9 onwards saves the insertion order of hashes. In fact in Ruby 2.1 - a hash of under 6 elements is stored as an array ;)

2.1.0 :001 > hsh = {'a' => 1, 'b' => 2, 'c' => 3}
 => {"a"=>1, "b"=>2, "c"=>3}

1.8.7-p374 :001 > hsh = {'a' => 1, 'b' => 2, 'c' => 3}
 => {"c"=>3, "b"=>2, "a"=>1} 

No comments:

Post a Comment