How to find Ruby method dependencies?

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

For example:

def foo puts "foo" end def foo2 foo end 

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

Is it possible? and if so, how? I can say that my question is about finding method dependencies.

+8
ruby
source share
2 answers

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

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

In Ruby 2.0, TracePoint is an excellent alternative.

+4
source share

The analysis of static code, especially the one you want to execute (listing all the methods called inside the method), is very difficult in ruby โ€‹โ€‹(almost impossible), because the language is dynamic and allows you to use very strong metaprogramming methods. Even the analyzer itself does not know the methods that are required until it tries to execute the code.

Example: calling eval with code read from a file.

+4
source share

All Articles