From: "Mauricio Fernández" Date: 2002-10-08T19:08:08+09:00 Subject: Re: Specifying local and external block parameters (that old chestnut) On Tue, Oct 08, 2002 at 06:31:03PM +0900, ts wrote: > >>>>> "M" == Mauricio =?iso-8859-1?Q?Fern=E1ndez?= writes: > > >> b = 12 > >> 1.times { b := 2 } > >> > >> `b := 2' will create a new block variable which shadow the previous > >> variable `b' > > M> matz said previously that in > M> (0...10).each { |x| ... } > M> the values would be assigned to x with ':=', that is, > M> x = ... > M> inside the block would use the local. Put otherwise, this is the same as > M> the "make them all local" solution w.r.t. block parameters! > > I know this, but you don't have || in my example. > > M> i = 0 > M> x = 'some value' > M> (0...10).each do |i| > M> x := 'surprise!' if i == 2 > M> x = i # uses local after the := is run > M> end > > M> x => 1 > > I don't see where is the problem > > pigeon% cat b.rb > #!./ruby > i = 0 > x = 'some value' > (0...10).each do |i| > x := 'surprise!' if i == 2 > x = i # uses local after the := is run > p x > end > p x > pigeon% > > pigeon% b.rb > ./b.rb:4: warning: shadowing variable `i' > ./b.rb:5: warning: shadowing variable `x' > 0 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > "some value" > pigeon% > Now I get it... it depends on whether Ruby sees ':=' in the *parsing* phase. I thought Ruby would begin to consider variables local after *executing* the ':=' statement. So I thought we'd get i = 0 x = 'some value' (0...10).each do |i| x := 'surprise!' if i == 2 x = i # uses local after the := is run p x end p x > pigeon% b.rb > ./b.rb:4: warning: shadowing variable `i' > ./b.rb:5: warning: shadowing variable `x' 0 1 2 3 4 5 6 7 8 9 1 because after i == 2 only the local 'x' would be assigned to, not the outer one. Of course this would be absolutely insane, but going the syntactic way seems so much better :) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com We come to bury DOS, not to praise it. -- Paul Vojta, vojta@math.berkeley.edu