Ruby also has a for loop!
It’s Ruby! The ; is unnecessary and was there to confuse with Python’s :.
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.
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.
Link to Ruby documentation for for
.