[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

DRYing a loop..

Josselin

12/19/2006 6:12:00 AM

I wrote that the newbie way, but is there any way to DRY it ?

closed_cities = []
0.step(9, 1) do |i|
closed_cities << City.find_by_id( city_25 [i] [0] )
i = i.next
end

if yes, is it only for fun , or does it impact performance (a lot ?)

thanks for your lights

joss

2 Answers

George

12/19/2006 6:34:00 AM

0

On 12/19/06, Josselin <josselin@wanadoo.fr> wrote:
> I wrote that the newbie way, but is there any way to DRY it ?
>
> closed_cities = []
> 0.step(9, 1) do |i|
> closed_cities << City.find_by_id( city_25 [i] [0] )
> i = i.next
> end
>
> if yes, is it only for fun , or does it impact performance (a lot ?)

Not sure where the "wet" bit is, but the "i = i.next" is redundant, as
'i' gets assigned on each call of the block.

Without knowing more about what each thing is/does, I'd recommend:

closed_cities = (0..9).map{|i| City.find_by_id(city_25[i][0])}

> thanks for your lights

Welcome!

Josselin

12/19/2006 8:16:00 AM

0

On 2006-12-19 07:33:40 +0100, "George Ogata" <george.ogata@gmail.com> said:

> (0..9).map{|i| City.find_by_id(city_25[i][0])}

thanks a lot.. that's it !! I forgot to look over the 'range' facility !!!