# Hash#keys, Hash#values order question

**URL:** <https://rubytalk.org/t/hash-keys-hash-values-order-question/40134>\
**Category:** ruby-talk\
**Created:** [23 August 2007 09:34 UTC](https://rubytalk.org/t/hash-keys-hash-values-order-question/40134 "2007-08-23T09:34:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronald\_Fischer1](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Ronald\_Fischer1](https://rubytalk.org/u/Ronald_Fischer1)\
**Post date:** [23 August 2007 09:34 UTC](https://rubytalk.org/t/hash-keys-hash-values-order-question/40134/1 "2007-08-23T09:34:49Z")

</div>

Let h be a Hash. Calculating the arrays

&nbsp;&nbsp;k=h.keys  
&nbsp;&nbsp;v=h.values

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

&nbsp;&nbsp;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

> **···**
>
> --  
> Ronald Fischer \<[ronald.fischer@venyon.com](mailto:ronald.fischer@venyon.com)\>  
> Phone: +49-89-452133-162

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [23 August 2007 10:11 UTC](https://rubytalk.org/t/hash-keys-hash-values-order-question/40134/2 "2007-08-23T10:11:41Z")

</div>

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

> **···**
>
> On 8/23/07, Ronald Fischer \<ronald.fischer@venyon.com\> wrote:
> 
> > Let h be a Hash. Calculating the arrays
> > 
> > &nbsp;&nbsp;k=h.keys  
> > &nbsp;&nbsp;v=h.values
> > 
> > under the assumption that h is not modified  
> > in between, is it guaranteed that for all  
> > indices n
> > 
> > &nbsp;&nbsp;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?
> 
> --  
> [...] as simple as possible, but no simpler.  
> -- Attributed to Albert Einstein

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 August 2007 13:21 UTC](https://rubytalk.org/t/hash-keys-hash-values-order-question/40134/3 "2007-08-23T13:21:47Z")

</div>

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

> **···**
>
> 2007/8/23, Ronald Fischer \<ronald.fischer@venyon.com\>:
> 
> > Let h be a Hash. Calculating the arrays
> > 
> > &nbsp;&nbsp;k=h.keys  
> > &nbsp;&nbsp;v=h.values
> > 
> > under the assumption that h is not modified  
> > in between, is it guaranteed that for all  
> > indices n
> > 
> > &nbsp;&nbsp;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?
