From: Daniel DeLorme Date: 2008-10-02T14:26:44+09:00 Subject: Re: ruby1.9 block scope Suraj Kurapati wrote: > Brian Candler wrote: >> def my_find(a) >> # res=nil not needed! >> a.each { |e| res = e if e =~ /foo/ } >> res >> end > > IMHO, the res=nil is necessary to preserve the semantics of a closure: > > A lambda/closure should *enclose* the variables around it into a neat > package. It should *not* emit new variables into its surroundings! > Otherwise, it would be something like an "opener" instead of a > "closure". If you're thinking in terms of lambdas and anonymous functions then I can see how you would think that. But I think of blocks more in terms of control structures. Variables defined inside a "if" or "while" statement continue existing outside of the if/while, and so should variables defined inside a block. I don't deny that that lambdas are useful, but I do think we have a conflict here: blocks are used both as control structures and as lambdas, and those two would benefit from having different scoping rules. -- Daniel