At runtime, I would like to inject a line of code at the - # <-- Injection point
class A
def some_method(a, b = 1, *c, d: 1, **e)
# <-- Injection point
# Code that does stuff
puts a
puts b
puts c
puts d
puts e
end
end
The reason I need this is so that I can use the Ruby Tracepoint Class to extract some run time information about the method signature.
Specifically the default parameter values in b = 1
and d: 1
I have looked at this question Inspect the default value for optional parameter in ruby's method? where a useful answer exists but want to inject the code that they suggest, dynamically.
I already have existing code that can extracts method signatures from ruby classes.
Example
Given this Class, which is defined with lots of methods using various parameterized signatures.
class A
def d; end
def e(a); end
def f(a, b = 1); end
def g(a, b = 1, c = 2); end
def h(*a); end
def i(a, b, *c); end
def j(**a); end
def k(a, *b, **c); end
def l(a, *b, **c, &d); end
def m(a:); end
def n(a:, b: 1); end
def p?; end
def z(a, b = 1, *c, d: 1, **e); end
end
I can run the following code to extract signature information. Unfortunately, it fails to resolve the default
parameter or named values.
class_definition = MethodSignatures.new(A.new)
class_definition.print