Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve caller's binding without extension.
class Binding
@@call_binding_stack = [ nil, TOPLEVEL_BINDING ]
class << self
def caller_binding
@@call_binding_stack[-2]
end
def push_caller_binding(binding)
@@call_binding_stack.push(binding)
end
def pop_caller_binding
@@call_binding_stack.pop
end
end
end
set_trace_func lambda { |event, file, line, id, binding, klass|
return if klass == Binding
case event
when 'call'
Binding.push_caller_binding(binding)
when 'return'
Binding.pop_caller_binding
end
}
def foo
eval("local_variables", Binding.caller_binding)
end
bar = 0
p foo # => [:bar]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.