Created
December 17, 2019 14:25
-
-
Save petitviolet/cc6e873b24f00c9726ee609d1e7a53c4 to your computer and use it in GitHub Desktop.
Tracing method calls using TracePoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Debugger | |
def initialize(events) | |
@tp = TracePoint.new(*(events || %i[call b_call raise])) do |tp| | |
tp.binding.irb | |
end | |
@tp.disable | |
end | |
def trace(&block) | |
@tp.enable | |
res = block.call | |
@tp.disable | |
res | |
end | |
def self.trace(events: nil, &block) | |
Debugger.new(events).trace(&block) | |
end | |
end | |
def method_a(args) | |
method_b(args) | |
end | |
def method_b(args) | |
p = -> a { method_c(a) } | |
p.call(args) | |
end | |
def method_c(args) | |
puts args | |
end | |
def main | |
Debugger.trace do | |
method_a([1, :x]) | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment