The widely adopted way is to use each
. Not only because is more Ruby-ish, but because from Ruby 1.8.7+ each
returns an Enumerator object, which can be used to do magic cool functional stuff.
Also: when in doubt, benchmark**
require "benchmark"
array = [*1..100_000]
Benchmark.bm(11) do |x|
x.report("for .. in") { array.each{ |i| i.succ } }
x.report("each") { for i in array; i.succ; end }
end
** If you find the 0.00000001 nanoseconds of performance gain in your code to be relevant, probably you shouldn't be using Ruby anyway.