# Arrays and Hashes

**URL:** <https://www.ruby-forum.com/t/arrays-and-hashes/191421>\
**Category:** Ruby\
**Created:** [July 12, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421 "2010-07-12T14:22:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/1 "2010-07-12T14:22:30Z")

</div>

In “Why’s poignant guide to Ruby” it mentions the following:

Unlike arrays, the items in a hash are not kept in a specific order.

Can you clarify on this?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 2:35pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/2 "2010-07-12T14:35:49Z")

</div>

Arrays have order because each one represents a sequence of objects.  
This  
guarantees that for a given array you’ll get consistent results using  
the  
“each” method:

a = [1,2,3]  
a.each {|n| puts n} #you’ll always get 1 2 and 3 in that order

Hashes, on the other hand, are more about letting you map some object,  
the  
key, to another object, the value. A single hash can have several of  
these  
mappings, but the “order” of the mappings is insignificant and therefore  
not  
necessarily consistent.

{:a =\> 1, :b =\> 2, :c =\> 3} == {:b =\> 2, :a =\> 1, :c =\> 3} # true

Although all of the the “each” methods provided by a hash (“each”,  
“each\_pair”, etc.) return items in a sequence, that sequence isn’t  
guaranteed to be the same every time.

Hope that helps.

-Jake

On Mon, Jul 12, 2010 at 8:22 AM, Abder-rahman Ali \<

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/3 "2010-07-12T15:16:49Z")

</div>

On Jul 12, 6:22 am, Abder-rahman Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Unlike arrays, the items in a hash are not kept in a specific order.

Note that this is no longer true in Ruby 1.9. Insertion order,  
including initialization, is maintained for Hashes:

Slim2:~ phrogz$ irb --simple-prompt

> > h = { a:1, z:7 }  
> > =\> {:a=\>1, :z=\>7}  
> > h[:f] = 14; h[:b] = 99  
> > =\> 99  
> > h[‘last’] = “this one”  
> > =\> “this one”  
> > h[-1] = “really last”  
> > =\> “really last”  
> > h.each{ |k,v| p [k,v] }  
> > [:a, 1]  
> > [:z, 7]  
> > [:f, 14]  
> > [:b, 99]  
> > [“last”, “this one”]  
> > [-1, “really last”]  
> > =\> {:a=\>1, :z=\>7, :f=\>14, :b=\>99, “last”=\>“this one”, -1=\>“really  
> > last”}  
> > h.keys  
> > =\> [:a, :z, :f, :b, “last”, -1]

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/4 "2010-07-12T21:01:43Z")

</div>

> I may not getting your point clear. What is it not applicable to Ruby  
> 1.9 from what Jake said?
> 
> Ruby 1.9 added a constraint that whenever you query for an iterator using  
> any of the Hashes each\* methods, you’ll always get the objects in the  
> same  
> order that they were inserted into that hash. Prior to that there were  
> no  
> promises about the order the elements would be yielded by the iterator.

-Jake

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/5 "2010-07-12T17:42:59Z")

</div>

Gavin K. wrote:

> On Jul 12, 6:22ï¿½am, Abder-rahman Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Unlike arrays, the items in a hash are not kept in a specific order.
> 
> Note that this is no longer true in Ruby 1.9. Insertion order,  
> including initialization, is maintained for Hashes:
> 
> Slim2:~ phrogz$ irb --simple-prompt
> 
> > > h = { a:1, z:7 }  
> > > =\> {:a=\>1, :z=\>7}  
> > > h[:f] = 14; h[:b] = 99  
> > > =\> 99  
> > > h[‘last’] = “this one”  
> > > =\> “this one”  
> > > h[-1] = “really last”  
> > > =\> “really last”  
> > > h.each{ |k,v| p [k,v] }  
> > > [:a, 1]  
> > > [:z, 7]  
> > > [:f, 14]  
> > > [:b, 99]  
> > > [“last”, “this one”]  
> > > [-1, “really last”]  
> > > =\> {:a=\>1, :z=\>7, :f=\>14, :b=\>99, “last”=\>“this one”, -1=\>“really  
> > > last”}  
> > > h.keys  
> > > =\> [:a, :z, :f, :b, “last”, -1]

Thanks a lot Gavin.

I may not getting your point clear. What is it not applicable to Ruby  
1.9 from what Jake said?

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 1:40am UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/6 "2010-07-13T01:40:57Z")

</div>

On 2010-07-12 12:01:20 -0700, Jacob M. said:

> -Jake

In practice, hash order (for 1.8) is deterministic _per machine_,  
meaning the hash will enumerate in the same order each time for you,  
but not in the same order as someone else’s machine.

That said, this must not be relied on (after all, it isn’t in the  
spec). One should not expect hashes to be ordered, and any code in Ruby  
1.8 that treats the order of a hash as significant is incorrect.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/7 "2010-07-12T21:05:22Z")

</div>

Jacob M. wrote:

> > I may not getting your point clear. What is it not applicable to Ruby  
> > 1.9 from what Jake said?
> > 
> > Ruby 1.9 added a constraint that whenever you query for an iterator using  
> > any of the Hashes each\* methods, you’ll always get the objects in the  
> > same  
> > order that they were inserted into that hash. Prior to that there were  
> > no  
> > promises about the order the elements would be yielded by the iterator.
> 
> -Jake

I got you Jake. Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:10am UTC](https://www.ruby-forum.com/t/arrays-and-hashes/191421/8 "2010-07-13T04:10:47Z")

</div>

Yes. An orderedhash object is present in libraries such as activesupport  
because this feature can be useful. Its not very often that you need it  
ordered but every now and then it can come in handy.

I sometimes wish ruby had the rich tapestry of collection types that  
smalltalks such as gemstone avail their users of. For example, a bag is  
an unordered collection. Some collections don’t allow multiples, others  
are faster… Etc

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails deploy  
process! Check it out now!
