From: Dennis Decker Jensen Date: 2001-03-13T03:26:02+09:00 Subject: [ruby-talk:12521] SV: Re: FEATURE REQUEST: 'my' local variables Mark Slagell wrote: > Still... this One Particular Behavior gets brought up by a pretty > significant portion of new ruby users who are confused or bothered by > it. It's the one real "fix" I really would like to see incorporated if > an agreeable (and hopefully fully backwards-compatible) syntax can be > settled on. Agreed. I didn't like the notion of 'my' but a more explicit notion would be beneficial. I find this very hard but will try anyway...: As far as I can see, there are currently two main suggestions: a) addition of another kind of block-scope b) addition of another variable-prefix for block-local variables ...And actually a third suggestion (c) but it seems to move towards the second suggestion (b). Ad a) ----- This was one of the first things in Ruby to confuse me until I read about the different precedences of binding (do...end vs. {...}, and vs. &&, etc.) What about prefixing do...end and {...} with the keyword "local"? Like this: z = 7; puts z # -> 7 [1, 2, 3].each local { |x, y, z| y=x*3; z=y*2; p z } # -> 6 12 18 puts z # -> 7 puts y # -> [NameError] y not initialized # y is strictly local and not accessible after end of block-scope # z is strictly local like y # but with access to surrounding scope like usual # x is like usual The position determines it. If each was reimplented to yield two arguments to the block, y would also become an usual local like x. When used with do...end (local do ... end), all of the locals would be visible afterwards (x, y and z), but z would still not be "hurt" and preserve its previous value which in this case is 7. Ad b) ----- loop { |a, b, c, :d, :c| ... } I don't like this, since they remind me of symbols and not of strictly local variables... loop { |a, b, c, ::d, ::c| ... ::d = ::d + 1; ... } It looks akward, but only because the syntax is new to Ruby. Unlike my suggestion earlier, no position determines which variables are strictly local. This I like! The concept is explicit and clear. But i would like a more readable syntax instead of something obscured. "::" reminds me of Perl-syntax a bit too much... Is "!" taken? ;-) I could also imagine loop { |a, b, c, !d, !c| ... !d = !d + 1; ... } # ;-) a lot of NOTs... However. The main question remains: Is the new syntax worth the added complexity? Maybe some other syntax?.... BTW: I haven't got a clue about the implementation and the rest, so thanks to Matz for keeping all of us straight on the road... :-) Dennis