Can someone list a synopsis of the new rules (or have I missed that?)
This thread got to be wayyyy too long to follow for me with all the
points and counterpoints.
(1) Block parameters - e.g. |i,j| - are always local to the block,
regardless of whether a variable with the same name already exists. e.g.
i = 0
[1,2,3].each { |i| # this is a *different* i (shadowing)
puts i
}
puts i
will print 1 2 3 0
I think Matz wants a compulsory warning if you shadow a variable in this way
(i.e. generated even if you don't specify '-w')
(2) Other assignments are always bound to the method's main local variable
scope, whether or not it already exists. For example:
[1,2,3].each { |i|
j = i
}
puts j
will print '3', and
[1,2,3].each { |i|
j = i if i > 3
}
puts j
will print 'nil'. The same behaviour is achieved in the current version of
Ruby if you put 'j = nil' before the loop.
So two distinctions will no longer exist:
- block parameters are local or bound?
=> they are always local
- variables defined within a block are local or bound?
=> they are always bound
Under the current rules, in both cases they are 'bound' if an assignment to
a variable of the same name occured anywhere before the block, and 'local'
if not.
Regards,
Brian.