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

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -