# On Hashes - How the hashes printing works?

**URL:** <https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910>\
**Category:** Ruby\
**Created:** [May 27, 2009, 10:16am UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910 "2009-05-27T10:16:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 27, 2009, 10:16am UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910/1 "2009-05-27T10:16:55Z")

</div>

Hi All,

I have my code as follows:-

people = {  
“torvalds”=\>{“lname”=\>“Torvalds”, “fname”=\>“Linus”,  
“job”=\>“maintainer”}, “matsumoto”=\>{“lname”=\>“Matsumoto”,  
“fname”=\>“Yukihiro”, “job”=\>“Ruby originator”},  
“litt”=\>{“lname”=\>“Litt”, “fname”=\>“Steve”, “job”=\>“troubleshooter”}  
}  
keys = people.keys  
for key in 0…keys.length  
print "key : ", keys[key], “\n”  
print "lname: ", people[keys[key]][“lname”], “\n”  
print "fname: ", people[keys[key]][“fname”], “\n”  
print "job : ", people[keys[key]][“job”], “\n”  
print “\n\n”  
end

O/p:-  
key : litt  
lname: Litt  
fname: Steve  
job : troubleshooter  
key : matsumoto  
lname: Matsumoto  
fname: Yukihiro  
job : Ruby originator  
key : torvalds  
lname: Torvalds  
fname: Linus  
job : maintainer

Question:-  
Why key of “litt” is printed first thought the first key of hash is  
“torvalds”?

Thanks and regards,  
Neela.

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 27, 2009, 10:26am UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910/2 "2009-05-27T10:26:34Z")

</div>

Hashes enumeration does not mirror insertion order.  
Hashes are not ordered:

> **[Hash table](https://en.wikipedia.org/wiki/Hash_table#Drawbacks)**
>
> In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.
> Ideally, the hash function will assign each key to a un...

Markus

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 27, 2009, 10:28am UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910/3 "2009-05-27T10:28:18Z")

</div>

Markus S. wrote:

> Hashes enumeration does not mirror insertion order.  
> Hashes are not ordered:
> 
> [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Drawbacks)
> 
> Markus

Thank you Markus.

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 28, 2009, 8:30am UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910/4 "2009-05-28T08:30:40Z")

</div>

Jou could use orderedhash gem to have hashes ordered like arrays for  
example if you really need it for some reason:  
[http://codeforpeople.com/lib/ruby/orderedhash/](http://codeforpeople.com/lib/ruby/orderedhash/)

Markus S. wrote:

> Hashes enumeration does not mirror insertion order.  
> Hashes are not ordered:
> 
> [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Drawbacks)
> 
> Markus

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 28, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/on-hashes-how-the-hashes-printing-works/168910/5 "2009-05-28T12:57:25Z")

</div>

Or maintain an “order of insertion key array”.
