Michael Neumann
11/16/2003 9:55:00 PM
On Mon, Nov 17, 2003 at 03:08:26AM +0900, Gavri Savio Fernandez wrote:
> Hi,
> I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour in ruby, namely that in the following code,
>
> def foo
> a = nil
> ary.each do |b|
> c = b
> a = b
> end
> end
>
>
> b and c are local to the each-block while the variable a which is referred to in the block is not. i assumed that in Ruby2, 'a' too would be local to the block and there would be different way to refer to the variable a which is outside the block's scope.
>
> However, when i checked out the next slide, "Horror Of Horrors!" :-) , in Ruby2, both a and c would be local to the entire method instead!
Simple rule (Ruby 2.0): Block parameters are local to the block,
everything else is not.
Consider the following example (Ruby 1.8):
a = 3
[1,2].each do |a|
p a
end
p a # => 2
This problem is fixed with 2.0 (a => 3).
I am not sure if the ability to mark variables explicitly as block-local
is consider by matz, e.g.:
ary.each do |b| <c, a>
end
or
ary.each do |b|
local c, a
end
For me, this looks not too bad and would fix all problems (hopefully).
This would mean that c and a are block local. By ommiting the "a"
inside < and >, one could access the outer a variable.
Many new things in Rite seem to be strange at the first look, and
luckily, there's still room for many discussions and improvements.
Regards,
Michael