# Hashes in 1.9 index

**URL:** <https://www.ruby-forum.com/t/hashes-in-1-9-index/193524>\
**Category:** Ruby\
**Created:** [August 19, 2010, 6:28am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524 "2010-08-19T06:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 19, 2010, 6:28am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524/1 "2010-08-19T06:28:26Z")

</div>

Since hashes in 1.9 retain their insert order, is there a built in way  
to get values by index as well as key?

For example:  
h = { a: 17, b: 18, c: 19 }

puts h[1]  
=\> 19

Obviously that can’t work, but there may be some other way I haven’t  
found by looking through the docs? At the moment I’m using this:

class Hash  
def by\_index( n )  
self.each\_with\_index { |item, index| break item if index == n }  
end  
end

Just wondering.

Regards,  
Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 19, 2010, 6:59am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524/2 "2010-08-19T06:59:26Z")

</div>

Iain B. wrote:

> Since hashes in 1.9 retain their insert order, is there a built in way  
> to get values by index as well as key?
> 
> For example:  
> h = { a: 17, b: 18, c: 19 }
> 
> puts h[1]  
> =\> 19
> 
> Obviously that can’t work, but there may be some other way I haven’t  
> found by looking through the docs? At the moment I’m using this:
> 
> class Hash  
> def by\_index( n )  
> self.each\_with\_index { |item, index| break item if index == n }  
> end  
> end
> 
> Just wondering.
> 
> Regards,  
> Iain

I think the closest to what you want is Hash#to\_a

Just like  
irb(main):011:0\> h = Hash[:a, 1, :b, 2, :c, 3]  
=\> {:a=\>1, :b=\>2, :c=\>3}  
irb(main):012:0\> h.to\_a[0][1]  
=\> 1  
irb(main):013:0\>

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 19, 2010, 8:22am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524/3 "2010-08-19T08:22:36Z")

</div>

On 19 Aug 2010, at 06:00, Paul H. wrote:

> 

Thanks, I’ll see how that pans out.

Regards,  
Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 19, 2010, 10:16am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524/4 "2010-08-19T10:16:34Z")

</div>

Iain B. wrote:

> Since hashes in 1.9 retain their insert order, is there a built in way  
> to get values by index as well as key?

No - I expect it just keeps them on a global linked list, in addition to  
the linked list for each hash bucket.

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [August 19, 2010, 9:08am UTC](https://www.ruby-forum.com/t/hashes-in-1-9-index/193524/5 "2010-08-19T09:08:51Z")

</div>

On 08/18/2010 09:28 PM, Iain B. wrote:

> Since hashes in 1.9 retain their insert order, is there a built in way to get values by index as well as key?
> 
> For example:  
> h = { a: 17, b: 18, c: 19 }
> 
> puts h[1]  
> =\> 19

Not much better than using #each\_with\_index or #to\_a:

> > h = { a: 17, b: 18, c: 19 }  
> > =\> {:a=\>17, :b=\>18, :c=\>19}
> 
> > h[h.keys[1]]  
> > =\> 18
