Ruby block variable scope within/out the method -


i following different results:

test 1:

def myblock   x = 1   y = 1   yield(x)   [x, y] end  myblock |x|  y = 10 end # => [1,1] 

test 2:

x = 1 y = 1  1.upto(2) |x|   y = 20 end [x,y] # => [1,20] 

variables created outside block available in block. why so?

that how scope of local variable defined. local variable can percolate block, not method definition.


Comments

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

javascript - Understanding ExtJS Framework Syntax -