2 ms·
To me the following is highly surprising: #assuming a is unbound at this point for a in 1..2 b = 2 end p a >> 2 I expect "a" to be free outside
by _pius 18y ago
To me the following is highly surprising:
#assuming a is unbound at this point
for a in 1..2
b = 2
end
p a
>> 2
I expect "a" to be free outside the loop in Ruby.
- mattmcknight 18y agoIn most of the OO languages I've used, declaring a variable in a loop header scopes it to the method, not the loop.
- SwellJoe 18y agofor my $a (1..2) { $b = 2; } print $a; Prints nothing. (Of course, with warnings or strict this complains about both $b and the second $a, as they're either never used or undefined.) Block scope is very nice. Maybe I don't use enough other OO languages to really grok why you'd want it any other way. What's the benefit to having method rather than block scope in such a context? I don't remember hating not having block scope in Python when I switched for about three or four years, but I didn't really use it in Perl at that point either, so I might miss it now that I do.