Do you know
for is almost a lump of syntactic sugar in ruby?
for in ruby is not like that in others.
When you write
for i in collection
Ruby translates it into sth like:
collection.each do |i|
But, the difference is that
each invokes the block of code repeatly, while
for is just a language construct.
So if the variable has existed before the block, that in the block is the same with the outside one. But if the variable appears only inside the block, it's local to the block, it can't be accessed anywhere outside the block.
There is a good example to explain it: https://gist.github.com/4109520