Search code examples
rubymethodsreceiverbacktrace

Getting receiver information from backtrace


From an error backtrace $!.backtrace, you can extract the method names for each step. I want to further extract the receiver of each method call. I am sure there is a way to do this because I saw this gem that has this function.

The gem mentioned above seems to be using Kernel.set_trace_func, and recording the binding information. But doing this for all method calls significantly slows down running of the program. How can I selectively record the binding information that is related to a call stack that leads to an error?


Solution

  • As far as I know, there's no way to get the binding information after an exception has been raised except by tracing the execution.

    A C extension could probably have access to the information available in the exception object (see https://github.com/ruby/ruby/blob/trunk/error.c#L552).

    You're pretty much out of luck here...