Ruby 2.0 block local variable #
Translated from matz blog, 2005-03-09
This is a topic I wanna talk about the meeting on yesterday1 but I didn’t because it didn’t warm up so much.
I’ve been thinking about Ruby’s block scope for a long time. I couldn’t single out easily in various ideas. In these days, It’s thought that I find the one hit the spot.
The specification is as follows.
- Block parameters are only local valiables.
- The scope of block parameter is block local.
- If the name of block parameter is overlapped outside local variable, then it’s error.(Shugo Maeda won.)
- You can set a variable that is enable in the block using syntax like ”; var” after block parameters.
- Overlapping of these variables is error.
- The scope of a variable appears first in the block is block local (according to the current).
- The reference to the variable in which the scope used in the method has ended is warned.
Though it’s different that I’ve declared about Ruby 2.0 upto now (especially, part of strong-ed), I seem the compatibility is higher and the usability is not worsend. Especially, dblack has been opposed applying the method scope in the point of “The scope of a variable appears first” might be pleased.
Now, as a result,
3.times {|i; a| a = 5 p [i, a] }
is ok.
3.times {|i; a=5| p [i, a] }
A syntax with initializing is more better. But ”|” is the operator might be in an expression. Thus,
- We give it up.
- Only primary one(literal, variable, expression bundled by parentheses, etc) be permitted at initialization.
It seems to become either.
As for the rest, it depends on whether we accept that the below is error or not.
3.times {|i; a=2+3| p [i, a] }
1 Matz organized a mini meeting Meetup for The Matsue Ruby Meetup Group . Matz belongs among Matsue Ruby Meetup Group .
chris2
Does this mean that
lambda { |@a, @b| }
is not allowed anymore?Also, I think there could appear problems with respect to code-generation when all local variables need to have unique names…
Daniel Berger
I can understand making a variable local to the block only. But requiring that it be unique? ICK ! NO!
Matt
I agree. I’d much rather they didn’t have to be unique.
Daniel Berger
Wait a minute. Is he saying that you must declare new variables as block parameters? Please, no.
Phil
sure the current method of declaring ‘shared’ variables to a block outside the block is a bit of a pain (or conversely, it can bite newbies when they use the same variable name in a block thinking it’s local to the block)... however, there have been various proposals for fixing this over the years, but the more proposals I see the more nervous they make me. I think I’m almost at the point now where I’d just prefer to keep things as they are.
Daniel Berger
Phil, I have to agree with you. The changes I wanted are minimal, but I’d gladly leave things where they are rather than implement what Matz is proposing here. A truly terrible idea IMHO .
matz
chris2: first, instance variables will not be allowed in the block parameters. second, they should be unique, as they should be now.
daniel: you can declare block local variables, but declaration is not mandatory.
kHebSkik
I honestly never understood why this is such a big deal in Ruby. But obviously I’m completely missing the point, here…
Austin
Matz, I have to admit that I don’t get it. Let’s say that I’ve got the following:
Will my use of
pdf
andtoc
be illegal, now? What happens if I had an “info” variable before the definition of the xref lambda? Do I need to do:xref = lambda { |info; pdf, toc| ... }
?If that’s the case, then I can safely say that I don’t like the proposal, as it will break existing code—that I just wrote, no less!—in nasty, nasty, nasty ways.
matz
Austine, the code is totally valid. Don’t worry.
The points are:
Austin
Sam
I’d go for the Scheme-style block parameters as suggested above.
Brian Mitchell
I checked the ChangeLog on 1.9. The experimental changes are in:
The best way to answer your questions would be to check the latest from CVS . I have a feeling that the original text was a bit more clear before translation.
Comments are closed for this entry.