From: Eric Mahurin Date: 2005-09-29T03:28:21+09:00 Subject: Re: creating independent lambdas in loops --- Martin DeMello wrote: > Ara.T.Howard wrote: > > On Thu, 29 Sep 2005, David A. Black wrote: > > > > > > a = 1 > > > method_call {|a| a = 2; b = 3 } > > > puts a # 1 > > > puts b # 3 > > > > oh gawd - that's horrible : variables exported from method > scope to the global > > (or enclosing perhaps?) one. eeks! > > Blocks exist in the caller's scope, not the method's, so I > don't really > see the problem here. > > martin I like it. The variable locality is independent of whether or not the variable existed in the outside scope (it is dependent on that now). Arguments would be local and other variables would be in the context of what contains the block. Period. With this you could easily create a localize method: def localize(&block) yield(*Array.new(block.arity)) # arity can't be <0 end Then this would localize x and y for you (initialize to nil): localize { |x,y| ... } __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com