Ordered hash: order breaks down pretty quickly

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.

-- I got the same results - 'dictionary' gem works properly -

The library seems broken - this is an opportunity to write a test case - identify the problem and submit a patch - first check lighthouse for a ticket.

Maybe the rails core team wants to rename Dictionary as OrderedHash

Dictionary is from the Ruby Facet's project.

{"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}

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

OrderedHash subclasses from Hash, and the inspect method wasn't overriden to inspect things in order (mea culpa!). if you iterate with each (or look at the keys) then you should see that things are in the right order

Fred

Frederick Cheung wrote:

OrderedHash subclasses from Hash, and the inspect method wasn't overriden to inspect things in order (mea culpa!). if you iterate with each (or look at the keys) then you should see that things are in the right order

Fred

ahhh...that was you? :slight_smile:

Yep, on closer inspection they are in the right order, and i monkeypatched the inspect method and it seems fine now:

module ActiveSupport   class OrderedHash     def inspect       "{" << self.collect{|k,v| "#{k.inspect} => #{v.inspect}"}.join(", ") << "}"     end   end end

thanks for clearing that up! max