It's almost syntax sugar. One difference is that, while for
would use the scope of the code around it, each
creates a separate scope within its block. Compare the following:
for i in (1..3)
x = i
end
p x # => 3
versus
(1..3).each do |i|
x = i
end
p x # => undefined local variable or method `x' for main:Object
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…