define_method exhibits the following behavior:
class TestClass
def exec_block(&block) ; yield ; end
end
TestClass.new.send(:exec_block) do ; puts self ; end
TestClass.send(:define_method, :bing) do ; puts self ; end
TestClass.new.bing
I do not understand that the block passed to define_method should be a closure. As such, it should be (at least according to my understanding) to fix the value of selfboth main, as shown in the call exec_block.
I understand that the block will become the body of the method, but I do not understand the reason for the behavior. Why does a block evaluate different things when used with different methods?
How can I reproduce the behavior of a block using define_methodfor other methods? for example, how do you write exec_blockit to output <TestClass:...>instead of `main '?
source
share