Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a Ruby method dependencies?

Tags:

ruby

Is there a way to get the list of methods that implement a Ruby method when this method is invoked?

For example:

def foo
  puts "foo"
end

def foo2
  foo
end

I want to know that when calling "foo2" it calls 1st "foo" and 2nd "puts" and the corresponding files these methods are defined into. (If "puts" calls other methods, I would like to know them too)

Is that possible? and if 'yes' how? I could say that my question is about finding the method dependencies.

like image 882
p.matsinopoulos Avatar asked Aug 20 '13 16:08

p.matsinopoulos


1 Answers

You can sort of get this using set_trace_func, but since Ruby is dynamic you would also need test code to call the methods so that the call order is printed.

set_trace_func proc { |event, filename, line, id, binding, klass| puts "#{klass}##{id}" }

In Ruby 2.0, TracePoint is a superior alternative.

like image 140
Max Avatar answered Oct 23 '22 11:10

Max