# Iterate over specific keys in a hash

**URL:** <https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587>\
**Category:** Ruby\
**Created:** [June 23, 2010, 11:23am UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587 "2010-06-23T11:23:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [June 23, 2010, 11:23am UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587/1 "2010-06-23T11:23:53Z")

</div>

Hi,  
I have a hash;

cars{ “car0” =\> “bmw”, “car1” =\> “seat”, “car3” =\> “mercedes”, “car4” =\>  
“renault”}

What I want is to extract just the values of car0 and car3.

Is there a way instead of doing cars.each\_value of doing  
cars.each\_value[0,3]?

Thank you in advance.

Ryan

---

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [June 23, 2010, 11:57am UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587/2 "2010-06-23T11:57:03Z")

</div>

Safas K. wrote:

> I have a hash;
> 
> cars{ “car0” =\> “bmw”, “car1” =\> “seat”, “car3” =\> “mercedes”, “car4” =\>  
> “renault”}
> 
> What I want is to extract just the values of car0 and car3.
> 
> Is there a way instead of doing cars.each\_value of doing  
> cars.each\_value[0,3]?

[cars[“car0”], cars[“car3”]]

I’m not sure what you would want [0,3] to do. For an Array, that would  
mean start at 0th element, count for 3 elements, i.e. would give you  
elements 0,1 and 2.

However, hashes are not indexed by integers. It’s a property of hashes  
in ruby 1.9 (only) that they maintain their insertion order when  
iterating, but you still can’t access the nth item directly.

So perhaps you want to use an Array instead:

> > cars = [“bmw”, “seat”, “mercedes”, “renault”]  
> > =\> [“bmw”, “seat”, “mercedes”, “renault”]
> 
> > cars.values\_at(0,3)  
> > =\> [“bmw”, “renault”]

---

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [June 23, 2010, 11:59am UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587/3 "2010-06-23T11:59:44Z")

</div>

On 23 juin 2010, at 11:23, Safas K. wrote:

> Hi,  
> I have a hash;
> 
> cars{ “car0” =\> “bmw”, “car1” =\> “seat”, “car3” =\> “mercedes”, “car4” =\>  
> “renault”}
> 
> What I want is to extract just the values of car0 and car3.

cars.values\_at(“car0”, “car3”)

# =\> [“bmw”, “mercedes”]

---

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [June 23, 2010, 12:03pm UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587/4 "2010-06-23T12:03:20Z")

</div>

Update: if you are interested at the values for particular keys, then  
you can also do

> > cars = { “car0” =\> “bmw”, “car1” =\> “seat”, “car3” =\> “mercedes”, “car4” =\>  
> > “renault”}
> 
> > cars.values\_at(“car0”,“car3”).each { |c| puts c }  
> > bmw  
> > mercedes  
> > =\> [“bmw”, “mercedes”]

If you really want to rely on the insertion order, which I’d strongly  
recommend against even under ruby 1.9, there’s a brute-force way:

cars.each\_value.with\_index do |c,i|  
next unless [0,3].include?(i)  
puts c  
end

Note that this will give you bmw and renault, because it ignores the  
keys completely.

---

<div class="post-metadata">

**Author:** ![razta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ec9cab/32.png) [@razta](https://www.ruby-forum.com/u/razta)\
**Post date:** [June 23, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/iterate-over-specific-keys-in-a-hash/190587/5 "2010-06-23T12:12:46Z")

</div>

Brian C.;

Thank you very much! The following was exactly what I needed;

> > cars.values\_at(“car0”,“car3”).each { |c| puts c }
