Hi, I've been confused by the definition of block binding in ruby. Here are two samples:
def redfine_a(&block)
a = 2
puts eval("a", block.binding)
block.call
end
a = "hello"
puts redfine_a {a}
This one could run with the expected result, while the second one:
def redefine_a(&block)
a= 2
puts eval("a", block.binding)
block.call
end
puts redefine_a{a= "hello"}
will complain:
undefined local variable or method `a' for main:Object (NameError)
After I removed the puts eval("a", block.binding)
, my code run:
def redefine_a(&block)
a= 2
block.call
end
puts redefine_a{a= "hello"}
Could anyone help me to understand where I am wrong?