From: Rick DeNatale Date: 2007-10-15T03:28:25+09:00 Subject: Re: Parameter in a block is not local? On 10/14/07, SpringFlowers AutumnMoon wrote: > I thought a iterator with a block is like an nameless function call... Similar, but not the same. Blocks are closures, so for example: a = 1 1.times {p a} will print 1 since a in the block refers to a in the outer scope. > so if it is a function call, the parameter is local. > but for the following, the output is surprising: > > a = 1 > p a > > 1.upto(10) {|a| p a} > > p a > > -------------- > > E:\>ruby test_iterator.rb > 1 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > 10 > > a is changed! > > but is it true that the "a" is not global to begin with.... > but then, isn't the "a" inside the block more local than the "a" > outside? In Ruby 1.8 a block argument acts like a local in the containing scope. This is changing in 1.9 http://eigenclass.org/hiki/Changes+in+Ruby+1.9#l8 -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/