# Ordered hash: order breaks down pretty quickly

**URL:** <https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965>\
**Category:** Rails\
**Created:** [June 10, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965 "2010-06-10T17:09:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 10, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965/1 "2010-06-10T17:09:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 10, 2010, 5:31pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965/2 "2010-06-10T17:31:41Z")

</div>

– 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 F.'s project.

–

gem install dictionary

[8:25:34]  
Successfully installed dictionary-1.0.0  
1 gem installed  
Installing ri documentation for dictionary-1.0.0…  
Installing RDoc documentation for dictionary-1.0.0…

./script/console

[8:25:43]  
Loading development environment (Rails 2.3.8)  
ree-1.8.7-2010.01 \> require ‘dictionary’  
ree-1.8.7-2010.01 \> 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]]  
d = Dictionary.new  
=\> {}

ree-1.8.7-2010.01 \> row.each {|e| d[e.first] = e.last}  
=\> [[“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]]

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 10, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965/3 "2010-06-10T17:32:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 10, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965/4 "2010-06-10T17:57:35Z")

</div>

Frederick C. 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? 🙂

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 10, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/ordered-hash-order-breaks-down-pretty-quickly/189965/5 "2010-06-10T17:35:47Z")

</div>

On Jun 10, 4:09 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
