I’m in rails 2.3.4, and i’m trying to convert an array into an ordered
hash. Since an ordered hash is kind of an array in disguise i thought
this would be simple. But the ordering is breaking down by the time i
add the third or fourth pair. Check it out:
row = [[“QID”, “8”], [“SID”, “21”], [“Question”, “Which number is the smallest?”], [“Right”, “-300”], [“Wrong1”, “-3”], [“Wrong2”, “-13”], [“Wrong3”, “-30”], [“50_50”, “1”], [“Checked”, nil], [“DBand5-7”, “NA”], [“DBand7-9”, “NA”], [“DBand9-11”, “H”], [“DBand11-14”, “E”], [“DBand14-16”, nil], [“Editor”, nil], [“Subject”, “Mathematics”], [“Question_Type”, “curriculum”], [“Keywords”, ““eggs, bacon, sausage and chips”, reading and writing, arithmetic”], [“UserLogin”, nil], [“Privacy”, nil], [“Official”, nil]]
=> [[“QID”, “8”], [“SID”, “21”], [“Question”, “Which number is the
smallest?”], [“Right”, “-300”], [“Wrong1”, “-3”], [“Wrong2”, “-13”],
[“Wrong3”, “-30”], [“50_50”, “1”], [“Checked”, nil], [“DBand5-7”, “NA”],
[“DBand7-9”, “NA”], [“DBand9-11”, “H”], [“DBand11-14”, “E”],
[“DBand14-16”, nil], [“Editor”, nil], [“Subject”, “Mathematics”],
[“Question_Type”, “curriculum”], [“Keywords”, ““eggs, bacon, sausage and
chips”, reading and writing, arithmetic”], [“UserLogin”, nil],
[“Privacy”, nil], [“Official”, nil]]ohash = ActiveSupport::OrderedHash.new
=> #<OrderedHash {}>
row.each{|pair| ohash[pair.first] = pair.last;puts “\n#{ohash.inspect}”};false
#<OrderedHash {“QID”=>“8”}>
#<OrderedHash {“QID”=>“8”, “SID”=>“21”}>
#<OrderedHash {“QID”=>“8”, “SID”=>“21”, “Question”=>“Which number is the
smallest?”}>
#<OrderedHash {“QID”=>“8”, “SID”=>“21”, “Right”=>"-300",
“Question”=>“Which number is the smallest?”}>
Here i’m onto the fourth pair and already the order is screwed up:
“Right” has jumped in before “Question”.
Can anyone explain what’s going wrong here? thanks - max
ps i don’t want to (re)start a debate on the merits (or lack thereof) of
ordered hashes thanks! Just want to understand this particular problem.