3 ms·
Yes I think so - your example will still print "after the block x is 5", but in that case that probably is what you would expect. Maybe my head has just gone
by fendale 18y ago
Yes I think so - your example will still print "after the block x is 5", but in that case that probably is what you would expect.
Maybe my head has just gone blank, but I cannot think of a way to get a local called 'x' inside that block that is in scope only within the block.
In Perl you would have done
my $x = 5;
foreach my $i in (1..5) {
my $x = $i;
...
}
And it would have given you a new 'block local' and left your original x alone, but I cannot think of how you can do the equivalent of 'my' in Ruby ...