# Nested loop

**URL:** <https://www.ruby-forum.com/t/nested-loop/145887>\
**Category:** Ruby\
**Created:** [August 14, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/nested-loop/145887 "2008-08-14T19:26:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/nested-loop/145887/1 "2008-08-14T19:26:04Z")

</div>

I know there is DRY way tow rite that, but I don’t remember how

residence\_rentals\_ids = []  
residences = user.franchise.residences  
for residence in residences  
residence\_rentals\_ids = residence.rentals.map {|rental|  
rental[:id] }.compact  
end

I tried unsuccessfully :

residence\_rentals\_ids = user.franchise.residences.each { |residence|  
residence.rentals.map { |rental| rental[:id}.compact }

thanks for your lights

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/nested-loop/145887/2 "2008-08-14T19:30:46Z")

</div>

On 14 août, 19:24, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> residence\_rentals\_ids = user.franchise.residences.each { |residence|  
> residence.rentals.map { |rental| rental[:id}.compact }
> 
> thanks for your lights
> 
> erwin

got it :  
residence\_rentals\_ids = []  
user.franchise.residences.each { |residence| residence.rentals.map { |  
rental| residence\_rentals\_ids \<\< rental[:id] }.compact }

but is there a way to avoid the residence\_rentals\_ids = [] line,  
including the array initialization into a block ?

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/nested-loop/145887/3 "2008-08-14T20:34:46Z")

</div>

On 8/14/08, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> residence\_rentals\_ids = []  
> user.franchise.residences.each { |residence| residence.rentals.map { |  
> rental| residence\_rentals\_ids \<\< rental[:id] }.compact }
> 
> but is there a way to avoid the residence\_rentals\_ids = [] line,  
> including the array initialization into a block ?

residence\_rentals\_ids = user.franchise.residences.map{ |residence|  
residence.rentals.map { |rental| rental[:id] }.compact }.flatten

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 14, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/nested-loop/145887/4 "2008-08-14T20:37:07Z")

</div>

On Thu, Aug 14, 2008 at 12:27 PM, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I tried unsuccessfully :  
> > user.franchise.residences.each { |residence| residence.rentals.map { |  
> > rental| residence\_rentals\_ids \<\< rental[:id] }.compact }
> 
> but is there a way to avoid the residence\_rentals\_ids = [] line,  
> including the array initialization into a block ?

Generally you don’t want a map without assigning the result to  
something.

Does this do what you want?

residence\_rental\_ids = user.franchise.residences.map{|residence|  
residence.rentals.map{|rental| rental[:id] } }.flatten

-Michael

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 16, 2008, 10:05am UTC](https://www.ruby-forum.com/t/nested-loop/145887/5 "2008-08-16T10:05:36Z")

</div>

On 14 août, 20:33, Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > residence\_rentals\_ids = []  
> > residence\_rental\_ids = user.franchise.residences.map{|residence|  
> > residence.rentals.map{|rental| rental[:id] } }.flatten
> 
> -Michael

that’s it… thanks a lot… I was missing the .flatten functionality
