From: Charles Comstock Date: 2004-04-18T07:54:12+09:00 Subject: Re: New Local Variable Scope rule David Alan Black wrote: > Hi -- > > ts writes: > > >>>>>>>"D" == David Alan Black writes: >> >>D> Going back to the original point (more or less): I'm just a little >>D> perplexed by the whole idea of closures (blocks, Procs, lambdas) >>D> having this compile-time effect on local variables in their context of >>D> creation. >> >> Well, actually this is at compile time that ruby make the difference >> between local, block-local variable and method call. >> >> If I've well understood, in 2.0 this is not changed : the only difference >> is how ruby make the difference between local and block-local variable. > > > That's my understanding too (see above). I'm just not entirely > comfortable with it. > > >> Now the worst case is perhaps this >> >>svg% cat b.rb >>#!/usr/bin/ruby >>thr = [] >>5.times do |i| >> thr << Thread.new do >> a = i >> Thread.stop >> puts a >> end >>end >> >>thr.each {|t| t.run } >>svg% >> >>svg% b.rb >>0 >>1 >>2 >>3 >>4 >>svg% >> >> I'm not sure but I think that for 2.0 all threads will give the same >> result. > > > I assume so; it's like your other example, where 2.0 behaves the way > pre-2.0 does with "a = nil" before the block. To avoid it I guess > you'd have to do: > > ... Thread.new do |a| # etc. > > to create a new 'a' each time. > > > David > Feels awk-ish to me, the way you have to declare local variables by stuffing them in the parameter list. And awk doesn't have the easy ability to convert a list into a parameter list. So what do you do if you have a parameter list you don't know how long it will be but you still need local variables? This method bothers me considerably. It may make it easier for short code, but I really don't mind stuffing a "var = nil" before doing something in a block, that to me seems logical. Kind of annoying, but logical. Charlie