Tracer

When you want to trace the entrance and exit of each method, tracer is the tool for you. In order to add method call/return tracing to your Ruby program, load the Tracer library using the command-line option -r tracer. Here is sample output from tracer:

% ruby -r tracer fact.rb 2 #0:fact.rb:1::-: def fact(n) #0:fact.rb:1:Module:>: def fact(n) #0:fact.rb:1:Module:<: def fact(n) #0:fact.rb:10::-: print fact(ARGV[0].to_i), "\n" #0:fact.rb:10:Array:>: print fact(ARGV[0].to_i), "\n" #0:fact.rb:10:Array:<: print fact(ARGV[0].to_i), "\n" #0:fact.rb:10:String:>: print fact(ARGV[0].to_i), "\n" #0:fact.rb:10:String:<: print fact(ARGV[0].to_i), "\n" #0:fact.rb:1:Object:>: def fact(n) #0:fact.rb:2:Object:-: return 1 if n == 0 #0:fact.rb:2:Fixnum:>: return 1 if n == 0 #0:fact.rb:2:Fixnum:<: return 1 if n == 0 #0:fact.rb:3:Object:-: f = 1 #0:fact.rb:4:Object:-: while n>0 #0:fact.rb:4:Fixnum:>: while n>0 #0:fact.rb:4:Fixnum:<: while n>0 #0:fact.rb:5:Object:-: f *= n #0:fact.rb:5:Fixnum:>: f *= n #0:fact.rb:5:Fixnum:<: f *= n #0:fact.rb:6:Object:-: n -= 1 #0:fact.rb:6:Fixnum:>: n -= 1 #0:fact.rb:6:Fixnum:<: n -= 1 #0:fact.rb:6:Fixnum:>: n -= 1 #0:fact.rb:6:Fixnum:<: n -= 1 #0:fact.rb:5:Object:-: f *= n #0:fact.rb:5:Fixnum:>: f *= n #0:fact.rb:5:Fixnum:<: f *= n #0:fact.rb:6:Object:-: n -= 1 #0:fact.rb:6:Fixnum:>: n -= 1 #0:fact.rb:6:Fixnum:<: n -= 1 #0:fact.rb:6:Fixnum:>: n -= 1 #0:fact.rb:6:Fixnum:<: n -= 1 #0:fact.rb:8:Object:-: return f #0:fact.rb:8:Object:<: return f #0:fact.rb:10:Kernel:>: ...

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.