# Problem with each\_key

**URL:** <https://www.ruby-forum.com/t/problem-with-each-key/172665>\
**Category:** Ruby\
**Created:** [July 24, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665 "2009-07-24T23:29:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asmo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@asmo](https://www.ruby-forum.com/u/asmo)\
**Post date:** [July 24, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665/1 "2009-07-24T23:29:41Z")

</div>

Hi,  
I use NetBeans as IDE for Ruby. I tested my script on JRuby and got  
result I expected. The problem concern Hash and each\_key iterator. I  
have diffrent result on Ruby 1.8.7.

Ruby 1.8.7:  
irb(main):008:0\> hash = { ‘a’ =\> 20, ‘b’ =\> 300, ‘k’ =\> 1 }  
=\> {“k”=\>1, “a”=\>20, “b”=\>300}  
irb(main):009:0\> hash.each\_key { |key| puts “key: #{key} #{hash[key]}” }  
key: k 1  
key: a 20  
key: b 300  
=\> {“k”=\>1, “a”=\>20, “b”=\>300}  
irb(main):010:0\>

In NetBeans on JRuby I wrote:  
hash = { ‘a’ =\> 20, ‘b’ =\> 300, ‘k’ =\> 1 }  
puts hash.each\_key { |key| puts “key: #{key} #{hash[key]}” }

Output:  
key: a 20  
key: b 300  
key: k 1  
a20b300k1

I would like to have the same sequence as in JRuby. Which iterator  
should I use? Thank you for help.

---

<div class="post-metadata">

**Author:** ![asmo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@asmo](https://www.ruby-forum.com/u/asmo)\
**Post date:** [July 24, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665/2 "2009-07-24T23:53:49Z")

</div>

Marcin GÃ³rski wrote:

> Hi,  
> I use NetBeans as IDE for Ruby. I tested my script on JRuby and got  
> result I expected. The problem concern Hash and each\_key iterator. I  
> have diffrent result on Ruby 1.8.7.  
> (snip)
> 
> I would like to have the same sequence as in JRuby. Which iterator  
> should I use? Thank you for help.

I don’t think you’re going to get it. In 1.8.7 hashes are by definition  
unordered and the actual order is implementation-specific.

---

<div class="post-metadata">

**Author:** ![asmo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@asmo](https://www.ruby-forum.com/u/asmo)\
**Post date:** [July 25, 2009, 1:51am UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665/3 "2009-07-25T01:51:46Z")

</div>

> I don’t think you’re going to get it. In 1.8.7 hashes are by definition  
> unordered and the actual order is implementation-specific.  
> Well I found information on JRuby 1.2 page:  
> “A 1.8.6 compatible Ruby interpreter written in 100% pure Java.”  
> I think that Ruby 1.8.7 and 1.8.6 shouldn’t differ very much.

---

<div class="post-metadata">

**Author:** ![asmo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@asmo](https://www.ruby-forum.com/u/asmo)\
**Post date:** [July 25, 2009, 3:35am UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665/4 "2009-07-25T03:35:27Z")

</div>

Rick Denatale wrote:

> 2009/7/24 Marcin Gï¿½rski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
> This means that you can’t rely on a particular order, so even though  
> JRuby enumerates this particular hash in that order, and 1.8.7  
> enumerates in a different order, they both are compatible with the  
> documented behavior.

Thank you for your answers. I solved my problem without relying on  
each\_key using additional arrays, uniq and has\_key?.

---

<div class="post-metadata">

**Author:** ![asmo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea666f/32.png) [@asmo](https://www.ruby-forum.com/u/asmo)\
**Post date:** [July 25, 2009, 2:35am UTC](https://www.ruby-forum.com/t/problem-with-each-key/172665/5 "2009-07-25T02:35:58Z")

</div>

2009/7/24 Marcin Górski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > I don’t think you’re going to get it. In 1.8.7 hashes are by definition  
> > unordered and the actual order is implementation-specific.  
> > Well I found information on JRuby 1.2 page:  
> > “A 1.8.6 compatible Ruby interpreter written in 100% pure Java.”  
> > I think that Ruby 1.8.7 and 1.8.6 shouldn’t differ very much.

Well they differ, although not in this regard. BUT

The point is than in Ruby \< 1.9, there is no specified guarantee about  
the order in which hashes enumerate their elements.

This means that you can’t rely on a particular order, so even though  
JRuby enumerates this particular hash in that order, and 1.8.7  
enumerates in a different order, they both are compatible with the  
documented behavior.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
