3

If method A is called by other methods,

Is there any way to know which function call it at run time.

I can grep the whole project to list all the possible calling path,

But if I can know it at run time, it'll be more helpful.

How can I get it by embedded something in method A

for example, the calling flow maybe

C() -> B() -> A()

H()-> G()-> B()-> A()

I wanna know the recent 3,or 5 method calls,

I want to trace the flow in my controllers and helpers in Rails project But I require the tracer and it showed me.

Routing Error

undefined method `on' for Tracer:Class

Try running rake routes for more information on available routes. 

Here's my code

      Tracer.on
  def generate_nonexisted_book(price)

   ~~~
  end
  Tracer.off
newBike
  • 14,385
  • 29
  • 109
  • 192

2 Answers2

8

Ruby has one stdlib called Tracer. I would demonstrate it with a small example s below :

require 'tracer'

Tracer.on
def a;end
def b; a ; end
def c; b ; end
c
Tracer.off

Let me now run the code :

(arup~>test)$ ruby -v c.rb
ruby 2.0.0p0 (2013-02-24 revision 39474) [i686-linux]
#0:c.rb:4::-: def a;end
#0:c.rb:5::-: def b; a ; end
#0:c.rb:6::-: def c; b ; end
#0:c.rb:7::-: c
#0:c.rb:6:Object:>: def c; b ; end
#0:c.rb:6:Object:-: def c; b ; end
#0:c.rb:5:Object:>: def b; a ; end
#0:c.rb:5:Object:-: def b; a ; end
#0:c.rb:4:Object:>: def a;end
#0:c.rb:4:Object:<: def a;end
#0:c.rb:5:Object:<: def b; a ; end
#0:c.rb:6:Object:<: def c; b ; end
#0:c.rb:8::-: Tracer.off
(arup~>test)$ 

Description of some symbolic notations here came in the output:

  • +>+ - call a Ruby method
  • - - execute code on a new line
  • +<+ - return from a Ruby method
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
1

Maybe the methods Kernel.caller or Kernel.caller_locations can help you:

http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-caller

http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-caller_locations

dx7
  • 804
  • 5
  • 11