Module: BindingOfCaller

Defined in:
lib/ext/boc.rb

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.binding_of_callerObject

module_function



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ext/boc.rb', line 30

def self.binding_of_caller
   cont = nil
   event_count = 0
   tracer = lambda do |event, file, line, id, binding, classname|
      event_count += 1
      if event_count == 4
         case event
         when "return", "line", "end"
            yield binding
            set_trace_func(nil)
         else
            error_msg = "\n" <<
            "Invalid use of binding_of_caller. One of the following:\n" <<
            "  (1) statements exist after the binding_of_caller block;\n" <<
            "  (2) the method using binding_of_caller appears inside\n" <<
            "      a method call;\n" <<
            "  (3) the method using binding_of_caller is called from the\n" << 
            "      last line of a block or global scope.\n" <<
            "See the documentation for binding_of_caller.\n"
            cont.call(nil, lambda { raise(ScriptError, error_msg) })
         end
      end
   end

   cont, error = callcc { |cc| cc }
   if cont
      set_trace_func(tracer)
   else
      error.call 
   end
end