Ruby Scope Iterator -
this example:
def test(name) names = %w(one 2 3 four) names.each |name| puts name end puts name end test 'zero' #out 1 2 3 4 four #this correct? should 0
is correct output of program?
thanks , sorry english
this expected behavior in ruby 1.8, ruby 1.9.x, blocks variables have scope local block.
in ruby 1.8, block-variable ends changing global binding, if present. that's why, changes value of name
variable. per, ruby 1.9.x, block-variable never changes it's container binding this. output:
> test('zero')
one
two
three
four
0
consider example:
x = 10 (1..10).each { |y| print x+y, " "} #=> 11 12 13 14 15 16 17 18 19 20
here, x
not block variable inside block, picks binding present outside , uses value.
Comments
Post a Comment