From: John W Higgins Date: 2009-10-27T04:15:59+09:00 Subject: Re: Hash Reverse ? --005045016b592b81880476db64c5 Content-Type: text/plain; charset=ISO-8859-1 Good Morning, On Mon, Oct 26, 2009 at 5:59 AM, salai wrote: > I am preparing for my orals exam in Ruby (my last chance.. , If I fail > the test, no more possible to study Computer Science related :-) ) > > http://codepad.org/foNb9Pd3 > > class Hash > def hash_revert > hash_new = Hash.new > self.each {|key,value| > if not hash_new.has_key?(key) then hash_new[value] = > key end > } > return hash_new > end > end > > h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"} > p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6} > > or Should I get this ?? > # => {"a"=>2, "b"=>1, "c"=>6} > > The answer can be different (and most likely will be) under 1.8 and 1.9 - in 1.8 order is not guaranteed - but in 1.9 it is (order of insertion into the hash) and therefore under 1.9 you will get the second option and apparantly under 1.8 you get your first. John --005045016b592b81880476db64c5--