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

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

Ansible warning on jinja2 braces on when -

html - How to custom Bootstrap grid height? -