在 Ruby 中,是否有在救援期间发生异常时创建的可用绑定?

发布于 2025-01-16 06:04:37 字数 444 浏览 2 评论 0原文

我遇到的确切痛点是我正在解析文件列表。每个文件有多行。当出现问题时,我想打印出当前文件名、当前行号、当前行和其他一些有趣的变量等内容,然后在大多数情况下退出,因为错误将出现在我需要的代码中有待增强。

大多数变量对于嵌套的#each 块来说是本地的。有多个地方可能会出错。

我想做的只是在 rescue 内有一个全局 beginrescueend 块有一个可用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2025-01-23 06:04:37

嗯,最简单的方法是在 begin ...rescue...end 块 之前定义要跟踪的所有变量。

然后,您可以在救援块中轻松访问它们:

variable = 0
another_variable = 0
begin
    3.times do |a|
        variable = a
        5.times do |b|
            another_variable = b
            if a == 2 and b == 4
                raise KeyError
            end
        end
    end
    at_exit do
        puts binding.eval 'variable'
        puts binding.eval 'another_variable'
    end
rescue KeyError
    puts variable
    puts another_variable
end

但是如果您想在异常发生之前获得最后的绑定 - 您的解决方案是使用TracePoint:
https://ruby-doc.org/core-2.5.0/TracePoint。 html

这允许您跟踪定义了您需要的局部变量的绑定,然后获取最后一个。当然,如果你使用这种方法,它会让你的程序变慢一点。使用示例:(

last_binding = nil
trace = TracePoint.new(:b_return) do |tp|
  last_binding = tp.binding if tp.binding.local_variable_defined?('variable')
end

trace.enable

begin
    3.times do |a|
        variable = a
        5.times do |b|
            another_variable = b
            if a == 2 and b == 4
                raise KeyError
            end
        end
    end
    at_exit do
        puts binding.eval 'variable'
        puts binding.eval 'another_variable'
    end
rescue KeyError
    trace.disable
    puts last_binding.eval 'another_variable'
end

您跟踪的最佳事件是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:

variable = 0
another_variable = 0
begin
    3.times do |a|
        variable = a
        5.times do |b|
            another_variable = b
            if a == 2 and b == 4
                raise KeyError
            end
        end
    end
    at_exit do
        puts binding.eval 'variable'
        puts binding.eval 'another_variable'
    end
rescue KeyError
    puts variable
    puts another_variable
end

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:

last_binding = nil
trace = TracePoint.new(:b_return) do |tp|
  last_binding = tp.binding if tp.binding.local_variable_defined?('variable')
end

trace.enable

begin
    3.times do |a|
        variable = a
        5.times do |b|
            another_variable = b
            if a == 2 and b == 4
                raise KeyError
            end
        end
    end
    at_exit do
        puts binding.eval 'variable'
        puts binding.eval 'another_variable'
    end
rescue KeyError
    trace.disable
    puts last_binding.eval 'another_variable'
end

(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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文