mortee wrote:
SpringFlowers AutumnMoon wrote:
[1,2,3,4,5].each {|a| p a}
is equivalent to
for a in [1,2,3,4,5]
p a
endso the "for" version clearly doesn't have a new scope for "a".
But in the newest Ruby 1.9, they are not equivalent any more?
No, they aren't the same even in Ruby 1.8.x either. With the do...end or
{...} syntax, any variables assigned in the block, but inexisting at the
point of the definition of the block, are actually local to the block.
I should have pointed out specifically the difference to the for ... in
... end syntax: any local variable created (assigned to) in the latter
construct become part of the enclosing context, so you can actually
create new local variables in such a loop for later use.
mortee