# Hash#keys, Hash#values order question

**URL:** <https://www.ruby-forum.com/t/hash-keys-hash-values-order-question/112796>\
**Category:** Ruby\
**Created:** [August 23, 2007, 11:36am UTC](https://www.ruby-forum.com/t/hash-keys-hash-values-order-question/112796 "2007-08-23T11:36:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 23, 2007, 11:36am UTC](https://www.ruby-forum.com/t/hash-keys-hash-values-order-question/112796/1 "2007-08-23T11:36:06Z")

</div>

Let h be a Hash. Calculating the arrays

k=h.keys  
v=h.values

under the assumption that h is not modified  
in between, is it guaranteed that for all  
indices n

h[k[n]]==v[n]

i.e. that Hash#values delivers the values  
in the same order than Hash#keys delivers  
the respective keys?

When playing around, I found this always  
the case, but can I rely on it?

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 23, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/hash-keys-hash-values-order-question/112796/2 "2007-08-23T12:13:11Z")

</div>

On 8/23/07, Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i.e. that Hash#values delivers the values  
> in the same order than Hash#keys delivers  
> the respective keys?
> 
> When playing around, I found this always  
> the case, but can I rely on it?  
> Looks like for 1.8.6, I just had a quick look at the sources and both  
> use  
> rb\_hash\_foreach, just with a different extractor function.  
> Now I cannot be sure if some internal events like GC might change the  
> structure of the underlying hash that could lead to different results,  
> would surprise me though…

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 23, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/hash-keys-hash-values-order-question/112796/3 "2007-08-23T15:22:38Z")

</div>

2007/8/23, Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> i.e. that Hash#values delivers the values  
> in the same order than Hash#keys delivers  
> the respective keys?
> 
> When playing around, I found this always  
> the case, but can I rely on it?

If you need them in the same order, then the safest method is to  
access them in one iteration, like:

irb(main):001:0\> ha={:foo=\>1, :bar=\>2, :baz=\>3}  
=\> {:foo=\>1, :bar=\>2, :baz=\>3}  
irb(main):004:0\> keys, values = ha.inject([[],[]]) {|(ke,va),(k,v)|  
[ke \<\< k, va \<\< v]}  
=\> [[:foo, :bar, :baz], [1, 2, 3]]  
irb(main):005:0\> keys  
=\> [:foo, :bar, :baz]  
irb(main):006:0\> values  
=\> [1, 2, 3]

HTH

robert
