# All possible ways of iterating

**URL:** <https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777>\
**Category:** Ruby\
**Created:** [September 2, 2009, 2:20am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777 "2009-09-02T02:20:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 2, 2009, 2:20am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/1 "2009-09-02T02:20:59Z")

</div>

Hey,

I am just curious as to what are all the possible methods of iterating  
through a code block in ruby.

Thanks.

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 2, 2009, 2:24am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/2 "2009-09-02T02:24:29Z")

</div>

> I am just curious as to what are all the possible methods of  
> iterating through a code block in ruby.

Ruby it a Turing complete language. There are, literally, an infinite  
number of ways to do it. (Consider continuations, for example…)

– MarkusQ

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 2, 2009, 3:29am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/3 "2009-09-02T03:29:29Z")

</div>

Am Mittwoch, 02. Sep 2009, 09:20:06 +0900 schrieb Mark F.:

> I am just curious as to what are all the possible methods of iterating  
> through a code block in ruby.

More than in APL.

Bertram

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 2, 2009, 3:40am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/4 "2009-09-02T03:40:02Z")

</div>

> I am just curious as to what are all the possible methods of iterating  
> through a code block in ruby.

We all praise the module Enumerable now.  
It is a really great module.

[http://www.ruby-doc.org/core/classes/Enumerable.html](http://www.ruby-doc.org/core/classes/Enumerable.html)

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 4, 2009, 10:59am UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/5 "2009-09-04T10:59:00Z")

</div>

2009/9/2 Mark F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am just curious as to what are all the possible methods of iterating  
> through a code block in ruby.

You do not exactly “iterate through a code block”. Instead, code  
blocks are used for some of the iteration idioms in Ruby.

Btw, why do you ask?

robert

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [September 3, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/all-possible-ways-of-iterating/174777/6 "2009-09-03T22:51:19Z")

</div>

On Tuesday 01 September 2009 07:20:06 pm Mark F. wrote:

> I am just curious as to what are all the possible methods of iterating  
> through a code block in ruby.

The way I think of it is, you start with while/until and for, and  
while/until  
can be used as a sort of ‘do while’ loop, too.

But normally, you wouldn’t use these, you’d use something like

[1,2,3].each do |x|

# do something with x

end

Specifically, look up Enumerable, and blocks in general. I don’t  
consider these  
to be separate methods of iterating, since you could easily implement  
them  
like this:

class Array  
def each  
if block\_given?  
for x in self  
yield x  
end  
else  
enum\_for  
end  
end  
end

So, the language is conceptually simpler – there are at most five ways  
of  
iterating, depending how you count (while, do-while, until, do-until,  
for).  
But that’s also not entirely honest, as things in the standard library  
are  
often written in C for speed, even if they’re _possible_ to write in  
Ruby.  
Each is no exception.

So, to answer your question, there are very few ways that are built into  
the  
language, though how many there are depends how you count. On the other  
hand,  
there are an infinite number of ways you could define yourself. For  
example:

def twice  
yield  
yield  
end

twice { puts ‘hello’ }
