From: Rick DeNatale Date: 2008-09-18T22:07:15+09:00 Subject: Re: Preserve insert order in a Hash ------=_Part_36108_23888411.1221743690551 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, Sep 18, 2008 at 8:42 AM, Brian Candler wrote: > > well, basically I just need a Hash to use String as indexes like > > > > sourceInfo = Hash.new > > sourceInfo["var1"]=123 > > sourceInfo["var2"]=2 > > sourceInfo["var3"]=3 > > sourceInfo["var4"]=23 > > What do you want to happen if sourceInfo["var2"] is assigned a second > time? Do you want to replace it where it originally was in the sequence, > or do you want to delete it and add the new value to the end? Or do you > want both elements to appear at the same time? Or doesn't it matter? FWIW, Ruby 1.9 seems to keep the original insertion order, reassiging a value to an existing key leaves the order unchanged: $ irb1.9 irb(main):001:0> a = {a: 1, b: 2, c: 3} => {:a=>1, :b=>2, :c=>3} irb(main):002:0> a.keys => [:a, :b, :c] irb(main):003:0> a[:b] = 4 => 4 irb(main):004:0> a.keys => [:a, :b, :c] irb(main):005:0> a => {:a=>1, :b=>4, :c=>3} irb(main):006:0> At least my somewhat out of date 1.9 does $ ruby1.9 -v ruby 1.9.0 (2008-03-21 revision 0) [i686-darwin9.2.2] -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_36108_23888411.1221743690551--