# DRYing a loop

**URL:** <https://www.ruby-forum.com/t/drying-a-loop/79935>\
**Category:** Ruby\
**Created:** [December 20, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/drying-a-loop/79935 "2006-12-20T18:33:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 20, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/drying-a-loop/79935/1 "2006-12-20T18:33:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 20, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/drying-a-loop/79935/2 "2006-12-20T19:10:16Z")

</div>

On 12/19/06, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ?)

i = i.next is unecessary, the step method updates the loop for you. Also  
you  
can use upto instead of step:

0.upto(9) do |i|  
closed\_cities \<\< City.find\_by\_id( city\_25[i][0] )  
end

Or instead of using upto you can use a range:

(0…9).each do |i|  
closed\_cities \<\< City.find\_by\_id( city\_25[i][0] )  
end

But now you realize, hey if I’m gonna use a range, I can use map, so now  
you  
make your code one line:

closed\_cities = (0…9).map { |i| City.find\_by\_id( city\_25[i][0] ) }

I would suggest checking out the docs for Range, as well as for step,  
upto,  
times each and map.

thanks for your lights

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 20, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/drying-a-loop/79935/3 "2006-12-20T19:16:35Z")

</div>

On 12/19/06, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!
