# How to do a for loop...and iterate a set number of times?

**URL:** <https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978>\
**Category:** Ruby\
**Created:** [January 29, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978 "2009-01-29T23:19:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 29, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/1 "2009-01-29T23:19:05Z")

</div>

So painfully basic, but I’m just starting Ruby and am coming to it from  
C/C++/Java, etc. and some of the syntax is unnatural to me, and yes,  
I’ve tried Google. I have an array of things and I want to access the  
first 10…how do I do that?

for thing in things #(how do I set the beginning and end of the loop?)  
#do something  
end

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 29, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/2 "2009-01-29T23:27:08Z")

</div>

one solution would be:  
for thing in things[0…9]  
…  
end

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 29, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/3 "2009-01-29T23:30:47Z")

</div>

El Jueves, 29 de Enero de 2009, Dan No escribiÃ³:

> for thing in things #(how do I set the beginning and end of the loop?)  
> #do something  
> end

things.each {|thing|  
#do something  
}

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 29, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/4 "2009-01-29T23:37:17Z")

</div>

Dan No wrote:

> So painfully basic, but I’m just starting Ruby and am coming to it from  
> C/C++/Java, etc. and some of the syntax is unnatural to me, and yes,  
> I’ve tried Google. I have an array of things and I want to access the  
> first 10…how do I do that?
> 
> for thing in things #(how do I set the beginning and end of the loop?)  
> #do something  
> end

Usually collections define an each method. Each call into the block gets  
the next element in the collection. You don’t have to worry about the  
beginning and end of the loop. The each method and its friends are  
considered the most idiomatic.

ary.each {|element| …}

Or if you just want the equivalent of a C for loop, use upto. The block  
argument is the current count, starting with ‘start’:

start.upto(finish) {|n| …}

Also there’s step, which lets you use an increment other than 1:

start.step(finish, incr) {|n| …}

It really depends on what you want to do.

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 30, 2009, 12:55am UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/5 "2009-01-30T00:55:26Z")

</div>

If you want the first 10, just do this:

ary[0…9]

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 30, 2009, 12:58am UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/6 "2009-01-30T00:58:58Z")

</div>

It actually requires a little bit different thinking because once you  
know idiomatic ruby, all the ‘language’ stuff you have to worry about  
becomes stuff you just don’t have to think about, like building new  
arrays pit of existin ones, splitting things up, etc

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 30, 2009, 6:46am UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/7 "2009-01-30T06:46:35Z")

</div>

Um the beginning is the beginning and the end is the end. It’s simple.  
You don’t have to manage it!

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 30, 2009, 1:00am UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/8 "2009-01-30T01:00:12Z")

</div>

On Thu, Jan 29, 2009 at 11:24 PM, Dominik H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> one solution would be:  
> for thing in things[0…9]  
> …  
> end  
> maybe  
> things.first( 10 ).each do |thing|  
> …  
> end  
> is a little bit more idiomatic.  
> Cheers  
> Robert

---

<div class="post-metadata">

**Author:** ![dann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c6cbf5/32.png) [@dann](https://www.ruby-forum.com/u/dann)\
**Post date:** [January 30, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/how-to-do-a-for-loop-and-iterate-a-set-number-of-times/158978/9 "2009-01-30T18:09:10Z")

</div>

On Fri, Jan 30, 2009 at 6:43 AM, Julian L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Um the beginning is the beginning and the end is the end. It’s simple. You  
> don’t have to manage it!

We shall however consider the possibility that the properties of  
Enumerable cannot be extended to metaphysics 🙂  
R.
