From: dblack@... Date: 2003-02-06T09:23:21+09:00 Subject: Re: Local variables & blocks Hi -- On Thu, 6 Feb 2003, Brian Candler wrote: > On Wed, Feb 05, 2003 at 10:23:52PM +0900, dblack@candle.superlink.net wrote: > > > So that x in > > > > > > a.each do |item| > > > x = ... > > > end > > > p x > > > > > > would not be a block local variable. The code above will work without > > > preceding "nil" assignment. > > > > I know I'm joining this thread rather late, but I have a minority > > opinion anyway, so it may not matter :-) > > > > I'm one of the few people who have never been bothered by the way > > things are now. But I realize it's going to change, so that's OK. > > > > However, the above example, where x comes into being in the block and > > then persists, looks and feels very strange to me. I suspect that if > > this is adopted, it won't be long before it starts showing up in lists > > of "Ruby Gotchas" and such. > > Well, it's a side-effect of the new rules. The alternatives would be: Yes, I was expressing concern about precisely that effect :-) > > For me, part of the problem is just the visual backwardness of it -- > > having something propagate from a right-hand field back into a > > left-hand field, which has a "shoe on wrong foot" feel to me. But > > more fundamentally, I've lost sight of what problem people are > > perceiving and trying to solve. > > > > Originally, the problem was shadowing. > > ... more importantly, lack of it. > > For me, the biggest problem with the current rules is the non-locality of > code, or 'action at a distance'. Take the following snippet: > > ... > obj.each { |i| > j = fn(i) > } > ... > > Is 'i' a block local variable? Is 'j' a block local variable? > > The answer now is: "it depends". It depends on whether you happen to have > used a local variable called 'i' or 'j' earlier in the method. You have to > scan your whole method to find out. If you rely on one behaviour or the > other - especially in threads, where you likely rely on i or j being local, > then things will break badly if you (or someone else maintaining your code) > adds a line or two using 'i' or 'j' as a local variable higher up. This is the part I've never understood. Even if you write a method with no blocks, you still have to know whether or not you've used a variable name. I'm not sure I really believe that people often add lines to methods (their own or, certainly, other people's) without familiarizing themselves with what's going in the method. I certainly don't believe that they should do so :-) And the presence or absence of blocks doesn't seem to me to have any special status in this connection. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav