First, you cannot implement puti and directly call puti a_var to get the output as a_var = value of a_var . Ruby sees only puti formal parameter names in the puti ; it cannot display the actual parameter names.
In some other language, such as C / C ++, you can use Macro to implement your puti . This is another story.
However, you can implement put :a_var with Continuation . In another question, β Can you use the eval code in the context of the caller in Ruby? Sony Santos provided caller_binding to get the caller's binding (something like a perl call function).
The implementation should be slightly modified because callcc returns the return value of the block when it is first returned. This way you get an instance of Continuation , not nil . Here is the updated version:
require 'continuation' if RUBY_VERSION >= '1.9.0' def caller_binding cc = nil
Please note that puti should not be the last statement of your program, otherwise the ruby ββinterpreter will puti immediately and the trace function will not have a chance to run. So the point of the last line is "owner's place".
Arie Xiao Mar 26 '13 at 14:43 2013-03-26 14:43
source share