From: Rick DeNatale Date: 2007-10-16T03:47:47+09:00 Subject: Re: Parameter in a block is not local? On 10/15/07, 7stud -- wrote: > SpringFlowers AutumnMoon wrote: > > > > I thought a iterator with a block is like an nameless function call... > > > > Here's another example(a modification of the example found in pickaxe2, > p 106): > > if false > data = "hello" > end > > (1..3).each {data = "goodbye"} > > puts data > > --output:-- > goodbye > > > That is equivalent to Java's call by value--but using references and C++ > reference semantics, and the flat scope is converted to spheric scope > with assignment semantics applying throughout any call by value passing. No it's not. no matter what 'spheric scope' means. Blocks are closures, they are NOT functions/methods. In the example the variable 'data' in the block is the same variable as 'data' in data = 'hello' In that sense this is no different than if false; data = 'hello';end data = 'goodbye' The variable definition is lexical, so it doesn't matter that the data='hello' statement didn't get executed, it's the fact that the parser saw it which caused it to be defined. And note that if you JUST had 1.times {a = "goodbye"} with no prior definition of a in the scope, you'd get a NameError when you ran it because a is undefined, and Ruby 1.8 doesn't allow new locals to be defined within a block. Ruby 1.9 has a feature marked EXPERIMENTAL which allows block locals to be defined by listing them in the |'s preceded by a ; after any block arguments. This might or might not survive. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/