Block binding in Ruby -


I am confusing with the definition of block binding in Ruby. Two samples are:

Red define_a (and block) a = 2 eval ("a", block.binding) block.call ends a = "Hello" redfine_a puts {a}

This can run with an expected result, while the second:

  def redefine_a (& block) a = 2 adds eval (" A ", Block.binding) block.call puts redefine_a {a =" hello "}  

will complain:

  undefined local variable or method For A main: Object (name-error)  

I have deleted , after "eval" ("a", block.binding) , My code runs:

def redefine_a (& amp; block) a = 2 puts block.call end redefine_a {a = "hello"}

Where am I?

block.binding is the environment in which the block is defined, with Before that he runs.


Comments