Ruby’s reflection API is very helpful in examining and altering the structure or state of ruby programs.
Method class allows us to introspect the methods or functions in ruby.
It encapsulates method and returns it as a method object.
In previous Ruby versions, the Method#inspect method displayed limited information that didn’t give much insights about the method.
class A def m(a, b=nil, *c, d:, e: nil, **rest, &block) end end b = A.new p b.method(:m) => #<Method: A#m>
As we can the
Method#inspect displays only the name of class and method name.
Ruby 2.7, improved the
added the method arguments to it.
This will particularly be helpful when inspecting the internals of libraries or gems in in debuggers, scripts or console.
class Awesome def m(a, b=nil, *c, d:, e: nil, **rest, &block) end end b = Awesome.new p b.method(:m) => #<Method: Awesome#m(a, b=..., *c, d:, e: ..., **rest, &block)>
As we can see the method prints out details of arguments in much intuitive and natural form just like the method definition.