在 Ruby 中,是否有在救援期间发生异常时创建的可用绑定?
我遇到的确切痛点是我正在解析文件列表。每个文件有多行。当出现问题时,我想打印出当前文件名、当前行号、当前行和其他一些有趣的变量等内容,然后在大多数情况下退出,因为错误将出现在我需要的代码中有待增强。
大多数变量对于嵌套的#each
块来说是本地的。有多个地方可能会出错。
我想做的只是在 rescue
内有一个全局 begin
、rescue
、end
块有一个可用的 Binding
,这样我就可以找出我有兴趣打印出来的各种变量。
这似乎是一个相当明显的 Ruby 类型的东西,所以我觉得很奇怪,我是第一个想要这样的东西的人。然而,我在 Ruby 文档的异常处理部分中没有看到任何 Binding
或闭包概念。通常这意味着我从根本上滥用了该语言的概念。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,最简单的方法是在
begin ...rescue...end 块
之前定义要跟踪的所有变量。然后,您可以在救援块中轻松访问它们:
但是如果您想在异常发生之前获得最后的
绑定
- 您的解决方案是使用TracePoint:https://ruby-doc.org/core-2.5.0/TracePoint。 html
这允许您跟踪定义了您需要的局部变量的绑定,然后获取最后一个。当然,如果你使用这种方法,它会让你的程序变慢一点。使用示例:(
您跟踪的最佳事件是
b_return
- 它发生在每个块结束之后,因此这将使您的跟踪时间达到最佳)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
binding
before exception occurs - your solution is to use TracePoint:https://ruby-doc.org/core-2.5.0/TracePoint.html
This 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)