# Question about hsh.each\_pair

**URL:** <https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863>\
**Category:** Ruby\
**Created:** [January 22, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863 "2008-01-22T22:07:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [January 22, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863/1 "2008-01-22T22:07:23Z")

</div>

I was wondering if the hash.each\_pair function messes with the order of  
my hash?

I am using it to parse some text and replace html entities and  
apparently it changes the order of my hash before iterating each  
pairs…

Here is some example code to explain what I mean:

rep = {  
“&” =\> “”",  
“\<” =\> “\<”  
…  
}

rep.each\_pair do |key, value|  
text.gsub(key, value)  
end

Now obviously, the substitution needs to be done in the correct order  
(i.e. & needs to be parsed before \<…)

But somehow, each\_pair changes the order of my hash before executing the  
do block, so things arent getting parsed in the correct order…

Any ideas as to why the hash seems to be re-ordered before the do block  
is executed, and how I can prevent this??

---

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [January 22, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863/2 "2008-01-22T22:18:46Z")

</div>

On Jan 22, 2008, at 3:07 PM, Jean-nicolas Jolivet wrote:

> Any ideas as to why the hash seems to be re-ordered before the do  
> block  
> is executed, and how I can prevent this??

In versions of Ruby prior to 1.9, hashes are intrinsically unordered–  
you can never rely on the order of things coming out of them.

Perhaps an array or arrays might work better?

Dave

---

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [January 22, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863/3 "2008-01-22T22:20:31Z")

</div>

> I was wondering if the hash.each\_pair function messes with the order of  
> my hash?

It’s not the each\_pair method, it’s the hash itself. Hashes are  
unordered - or more specifically, ordered by the hash value of the  
key, which is unrelated to the order in which you added items.

> I am using it to parse some text and replace html entities and  
> apparently it changes the order of my hash before iterating each  
> pairs…  
> Now obviously, the substitution needs to be done in the correct order  
> (i.e. & needs to be parsed before \<…)
> 
> Any ideas as to why the hash seems to be re-ordered before the do block  
> is executed, and how I can prevent this??

You could try a nested array instead  
rep = [[“&”, “”"], ["\<","\<"] \> …]  
rep.each do |key, value|  
text.gsub(key, value)  
end

or, maybe more simply:  
require ‘CGI’  
CGI.escapeHTML(text)

---

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [January 22, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863/4 "2008-01-22T22:29:25Z")

</div>

Thanks a lot to the 3 of you! This is exactly the information I was  
looking for!

SilverTab

---

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [January 22, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/question-about-hsh-each-pair/126863/5 "2008-01-22T22:21:42Z")

</div>

Alle Tuesday 22 January 2008, Jean-nicolas Jolivet ha scritto:

> ```
> "&" => "&quot;",
> 
> ```
> 
> (i.e. & needs to be parsed before \<…)
> 
> But somehow, each\_pair changes the order of my hash before executing the  
> do block, so things arent getting parsed in the correct order…
> 
> Any ideas as to why the hash seems to be re-ordered before the do block  
> is executed, and how I can prevent this??

Hashes in ruby don’t keep order (see the ri documentation for Hash). In  
this  
case, you can use a nested array:

rep = [  
["&", “”"],  
["\<", "\<],  
…  
]

or use a hash to store the pairs and an array to store the order:

rep = {  
“&” =\> “”",  
“\<” =\> “\<”  
…  
}

order = ["&", “\<”, …]

then do:

order.each{|i| text.gsub(i, rep[i])}

I hope this helps

Stefano
