# Hash#each with nested array as value

**URL:** <https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520>\
**Category:** Ruby\
**Created:** [October 17, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520 "2007-10-17T17:45:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [October 17, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520/1 "2007-10-17T17:45:45Z")

</div>

I have a hash where the values are nested arrays. I’d like to be able to  
iterate over the hash and have 3 vars available in the block: key,  
value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t  
work. Is there a better way to do it than the last line below? Thanks.

Earle

* * *

irb(main):001:0\> require ‘pp’  
=\> true  
irb(main):002:0\> h = {0 =\> [[1], [2]]}  
=\> {0=\>[[1], [2]]}  
irb(main):003:0\> h.each {|a, b| pp a, b}  
0  
[[1], [2]]  
=\> {0=\>[[1], [2]]}  
irb(main):004:0\> h.each {|a, b, c| pp a, b, c}  
0  
[[1], [2]]  
nil  
=\> {0=\>[[1], [2]]}  
irb(main):005:0\> h.each {|a, b| pp a, b[0], b[1]}  
0  
[1]  
[2]  
=\> {0=\>[[1], [2]]}

* * *

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [October 17, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520/2 "2007-10-17T18:00:50Z")

</div>

On Oct 17, 9:45 am, Earle C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a hash where the values are nested arrays. I’d like to be able to  
> iterate over the hash and have 3 vars available in the block: key,  
> value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t  
> work.

Not much better, but:

irb(main):006:0\> h = { :a =\> [:foo, :bar] }  
=\> {:a=\>[:foo, :bar]}

irb(main):007:0\> h.each{ |key,pair| a,b = \*pair; p key, a, b }  
:a  
:foo  
:bar

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [October 17, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520/3 "2007-10-17T18:08:28Z")

</div>

On 10/17/07, Earle C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a hash where the values are nested arrays. I’d like to be able to  
> iterate over the hash and have 3 vars available in the block: key,  
> value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t  
> work. Is there a better way to do it than the last line below? Thanks.
> 
> Earle

Use parentheses.

> > h = [0 =\> [[1],[2]]}  
> > h.each{|k,(a,b)| p k, a, b}  
> > 0  
> > [1]  
> > [2]  
> > =\> {0=\>[[1], [2]]}

Regards,

Gordon

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [October 17, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/hash-each-with-nested-array-as-value/117520/4 "2007-10-17T20:09:58Z")

</div>

Gordon T. wrote:

> On 10/17/07, Earle C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a hash where the values are nested arrays. I’d like to be able to  
> > iterate over the hash and have 3 vars available in the block: key,  
> > value[0], and value[1]. As you can see, h.each {|a,b,c|…} doesn’t  
> > work. Is there a better way to do it than the last line below? Thanks.
> > 
> > Earle
> 
> Use parentheses.
> 
> > > h = [0 =\> [[1],[2]]}  
> > > h.each{|k,(a,b)| p k, a, b}  
> > > 0  
> > > [1]  
> > > [2]  
> > > =\> {0=\>[[1], [2]]}
> 
> Regards,
> 
> Gordon

Perfect. Thanks, Gordon.

Earle
