From: Robert Klemme Date: 2004-04-14T17:14:16+09:00 Subject: Re: New Local Variable Scope rule "Shashank Date" schrieb im Newsbeitrag news:c5id4r$22o3g$1@ID-194283.news.uni-berlin.de... > In one of Matz's slides at RubyConf , > > (http://www.rubyist.net/~matz/slides/rc2003/mgp00012.html) > > he mentions his New Local Variable Scope rule as follows: > > #--------------------------------------------------------------- > def foo > a = nil > ary.each do |b| > # b is block local > c = b > a = b > # a and c are local to the method > end > # a and c available here > end > #--------------------------------------------------------------- > > I was wondering what would happen if it was written as follows: > > #--------------------------------------------------------------- > def foo(&block) > a=nil; > ary.each(&block) > end; > > foo(lambda {|b| a=b; c=b}) > #--------------------------------------------------------------- First of all both examples are erroneous IMHO because ary is not defined in foo(). > In other words, in general, what is the advantage of having variables in > closures leak out? Your second example will not modify the a defined in foo() because the binding takes place at the point of invocation, i.e. another scope. If you do def foo(&block) a=nil; ary.each(&block) puts "a=#{a.inspect}" end; foo(lambda {|b| a=b; c=b}) puts "a*#{a.inspect}" You'll see a=nil a* Regards robert