The precise pain point I'm having is I am parsing a list of files. Each file has multiple lines. When something goes wrong, I'd like to print out such things as the current file name, the current line number, the current line, and some other interesting variables and then exit in most cases since the error will be in my code that needs to be enhanced.
Most of the variables are local to the #each blocks that are nested. There are multiple places something could go wrong.
What I'd like to do is just have one global begin, rescue, end block and within the rescue have a Binding available to me so I could dig out the various variables I'm interested in printing out.
This seems like a rather obvious Ruby type thing so I find it odd that I'm the first guy who would want such a thing. Yet, I don't see any Binding or closure concepts in the exception handling parts of Ruby's documentation. Usually this means I'm radically misusing the concepts of the language.
Well, the easiest way is to define all variables that you want to track before your
begin ... rescue ... end block.You can then easily access them within the rescue block:
But if you want to get last
bindingbefore exception occurs - your solution is to use TracePoint: https://ruby-doc.org/core-2.5.0/TracePoint.htmlThis allows u to track bindings where local variable that u need is defined and then get last of them. Of course, if you will use this method it will make your program a little bit slower. The example of usage:
(The best event for u to track is
b_return- it occurs after each block's ending, so this will make your tracking time-optimal)