Ruby 2.0 block local variable

明日夜に Redhanded に載せようかと思います。

This is a topic I wanna talk about the meeting on yesterday[1] 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 *), 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]

fn1. Matz organized a mini meeting "Meetup for The Matsue Ruby Meetup Group": . Matz belongs among "Matsue Ruby Meetup Group": .