From: "David A. Black" Date: 2009-02-13T03:04:09+09:00 Subject: Hash order (was: Re: If you are unhappy with the direction of Ruby 1.8.7+, respond) Hi -- On Fri, 13 Feb 2009, James Coglan wrote: >> >> >> >> What do you mean with "Hash ordering"? >> An order for Hash instances? Hash doesn't define a <=> method. >> Or iteration order of Hash elements? That wasn't defined >> in 1.8.6 (I don't know if it is in 1.8.7). > > > > In 1.8.6, iteration order would be the same as the order that the keys > appear in the source. To work with 1.8.7, you need to create an empty hash > and add keys one by one rather than putting them all in a hash literal (see > the patch). Hashes aren't ordered at all in 1.8, as far as I know. This works the same in 1.8.6 and 1.8.7: irb(main):002:0> h = {3,4,1,2} => {1=>2, 3=>4} irb(main):003:0> h.each {|*x| p x } [[1, 2]] [[3, 4]] => {1=>2, 3=>4} and here's a 1.8.7 case with individual key insertion: irb(main):001:0> h = {} => {} irb(main):002:0> h[3] = 4 => 4 irb(main):003:0> h[1] = 2 => 2 irb(main):004:0> h.each {|*x| p x } [[1, 2]] [[3, 4]] David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!