Never ever use for
it may cause almost untraceable bugs.
Don't be fooled, this is not about idiomatic code or style issues. Ruby's implementation of for
has a serious flaw and should not be used.
Here is an example where for
introduces a bug,
class Library
def initialize
@ary = []
end
def method_with_block(&block)
@ary << block
end
def method_that_uses_these_blocks
@ary.map(&:call)
end
end
lib = Library.new
for n in %w{foo bar quz}
lib.method_with_block { n }
end
puts lib.method_that_uses_these_blocks
Prints
quz
quz
quz
Using %w{foo bar quz}.each { |n| ... }
prints
foo
bar
quz
Why?
In a for
loop the variable n
is defined once and only and then that one definition is use for all iterations. Hence each blocks refer to the same n
which has a value of quz
by the time the loop ends. Bug!
In an each
loop a fresh variable n
is defined for each iteration, for example above the variable n
is defined three separate times. Hence each block refer to a separate n
with the correct values.