0037 For_loop

Ruby also has a for loop!

It’s Ruby! The ; is unnecessary and was there to confuse with Python’s :.

0037-for_loop_1.png

The for loop delegates the call to each. We can therefore iterate only with Enumerable. And just like each, the for loop returns the Enumerable. Unlike while, there is no one-line syntax.

0037-for_loop_2.png

The major difference with each is that it doesn’t create a new scope for variables. So be careful if you need them in another context.

0037-for_loop_3.png

Link to Ruby documentation for for.