You can introduce a new block for masking the outer variable
tmp = 0
for i in (1..5) do
proc do |;tmp|
puts i
tmp = i
end[]
end
This is awful. The difference between for and each is that the iteration variable in a for loop pollutes the outer scope.
x = []
# i has same scope as x
for i in (1..3)
# closure captures i outside the loop scope, so...
x << lambda { i }
end
# WAT
x.map(&:call) # [3, 3, 3]
x = []
(1..3).each { |i| x << lambda { i } }
# sanity restored
x.map(&:call) # [1, 2, 3]
Using my hack above to make your for act more like an each makes already confusing behavior even more confusing. Better to avoid for entirely.
forloop as i do in other programming languages, to keep a common pattern. It would be a substantial benefit to be able to definire local variables in the loop, i don't think i need to argument why it is so.tmpis indeed not modified. The block-local variabletmpmasks it. How is this is not what you want?