[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nested loop

Erwin

8/14/2008 5:24:00 PM

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


3 Answers

Erwin

8/14/2008 5:28:00 PM

0

On 14 août, 19:24, Erwin <yves_duf...@mac.com> wrote:
> 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

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 ?


Adam Shelly

8/14/2008 6:31:00 PM

0

On 8/14/08, Erwin <yves_dufour@mac.com> 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

Erwin

8/16/2008 8:01:00 AM

0

On 14 août, 20:33, Michael Libby <michael.c.li...@gmail.com> wrote:
> On Thu, Aug 14, 2008 at 12:27 PM,Erwin<yves_duf...@mac.com> wrote:
> > On 14 août, 19:24,Erwin<yves_duf...@mac.com> wrote:
> >> 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
>
> > 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 ?
>
> 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

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