Usually, when we create a proc, the proc object captures the variables that is in the creation context. However, in Symbol#to_proc, the object obj that is in:
Proc.new {|obj| obj.send(self) }
is not in the surrounding context of the proc object. At this point there is no 'obj' object. So, within the Symbol class to_proc
method, there is no obj. When the & ampersand trigger is executed, the map
method yields a block variable, this block variable gets associated to
the obj object. This is a subtle concept. We can bind values to the block variable in the proc object at run-time. This is a very powerful concept we can use when we develop libraries.
No comments:
Post a Comment