Is it preferable not to concatenate each
and with with_index
like this
array.each_with_index { ... }
or is concatenation perfectly acceptable?
array.each.with_index { ... }
Is it preferable not to concatenate each
and with with_index
like this
array.each_with_index { ... }
or is concatenation perfectly acceptable?
array.each.with_index { ... }
Both of these forms is correct and will work. In the case listed however, the first form is preferred as it uses the purpose built method for this task.
The second form should normally be reserved for methods that do not have a with index option as in this silly bit of code:
['a', 'b', 'c', 'd'].select.with_index {|_d, i| (i%2)==0}
which has as output:
["a", "c"]