1

Ruby has support for the for i in a syntax, but no one really uses it. One of the design principles of Ruby is it's readability, and I find

for i in a
  ...
end

much more readable (and easier to type) than

a.each do |i|
  ...
end

Why is it not preferred? Do others not also find it more readable? Is it for consistency reasons, e.g., relation with other functions like each_with_index and variable scoping?

sawa
  • 165,429
  • 45
  • 277
  • 381
Sebastian Carroll
  • 1,386
  • 12
  • 12
  • I think you should read about ruby blocks first. And this question: http://stackoverflow.com/questions/3294509/for-vs-each-in-ruby – konart Jun 02 '15 at 10:09
  • 2
    The chances are that you find things more readable once you have trained yourself to read them. There doesn't seem any inherent superiority to either syntax to me. Other than that, it is mainly opinion, but the `.each` syntax falls into line more cleanly than `for` with other Ruby idioms. Take a look at the other iterators on `Array` and you will find not only that `.each` fits in alongside them, but that you'll just as often use one of the others because it does what you really want when working through the array. You rarely *just* want to set an iterator. – Neil Slater Jun 02 '15 at 10:20
  • @konart I have and I think I understand the differences. This is what I meant by 'variable scoping' in the question. Is this the sole reason it isn't used? – Sebastian Carroll Jun 02 '15 at 10:20
  • @Sebastian Carroll consistency as well. – konart Jun 02 '15 at 10:21
  • 1
    I use `Enumerable#each` because it's just a method call, not another syntax, and it's more OO. – Aetherus Jun 02 '15 at 10:23
  • I find the `each` more readable, more consistent, and more practical. – Dave Newton Jun 02 '15 at 11:17

2 Answers2

2

For me, it's uniform syntax. When I am going to do something with array a, I just start typing a.. What am I going to do? Filter? a.select; determine the size? a.size. Iterate? a.each etc.

Iterating an array is nothing special and requires no special language construct. Also eachwithout an immediate block results in an Enumerator, which can be chained with other methods.

Then again, Ruby does have a fine for i in a loop. So if you are more comfortable with a for loop: by all means, use a for loop.

steenslag
  • 79,051
  • 16
  • 138
  • 171
1

Because for loop is less objective and more limited in use.

From the OOP point of view, possibility of iterating through elements of an object is a property/characteristic of that object. Using explicit "outer" way either with a global function or a keywords find redundant, inconsistent and less objective (encapsulation).

for is also more limited, it allows only iterate through object elements. Enumerator#each on the other hand allows you much more. You may play with enumerator object like chaining with other methods before values unpacking, passing named blocks, methods converted to procs etc.

David Unric
  • 7,421
  • 1
  • 37
  • 65