I think the other question already answers this, does not it? The only difference between the two is that in for
the variable that is used to do the iteration continues in scope when for
ends.
Internally, for
is equal to each
. I personally prefer each
because it leaves no side effects and because it seems more "natural" within the Ruby style. for
could be preferred if there was any intention to use the variable after the loop, to return the last element or something. But it can also be argued that this does not help with legibility. If there is no performance gain, one should always prefer the more readable and simpler code. I believe that choosing between one or the other is a matter of style. Ruby programmers usually prefer each
, but beginner programmers who come from other languages like JavaScript may initially find it easier to understand for
.