From: mortee Date: 2007-10-15T10:20:19+09:00 Subject: Re: Parameter in a block is not local? SpringFlowers AutumnMoon wrote: > So in a way, think of a code block like a "if" statement? (no new local > scope) > > Come to think about it, since > > [1,2,3,4,5].each {|a| p a} > > is equivalent to > > for a in [1,2,3,4,5] > p a > end > > so 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. AFAIK, in 1.9, the formal parameters of the block, specified between ||'s, are considered unconditionally block-local *too*. In 1.8.x, those are treated like any other local variables in the block: they refer to the same as the enclosing block if pre-existing, and refer to block-local variables otherwise. mortee