Getting the name of a specific method

I know that I can capture the moment the method is defined with set_trace_func .

 set_trace_func ->event, file, line, method, binding, klass{ if event == "c-call" and method == :method_added # The moment of method definition end } 
  • Is it possible to write the name of the method defined at this moment? I know that a class can be captured by eval("self", binding) . What code can be placed inside the above block to capture the method name?
  • Is it possible to get the format of the arguments for the method being defined (the required arguments, the remaining arguments and their names as they are in the source)?
+4
source share
2 answers

Outside of set_trace_func you can use Module.method_added :

 class Test def self.method_added(method_name) puts "#{method_name} added to #{self}" end def foo "foo" end end $ ruby test.rb # => foo added to Test 
+1
source

Check the documentation.

Kernel.set_trace_func proc allows you to catch the id parameter. This is the time - this is the name of the function.

However, by studying your example, you can also get the current current method using eval("__method__", binding) ... but I think this only gets the methods that you defined in your classes.

0
source

Source: https://habr.com/ru/post/1438241/


All Articles