什么是红宝石中的block_singleton_method? 为什么我在退出时收到此错误



下面是由 gtk 关闭操作调用的代码 -->

def on_main_window_destroy(object)
begin
$client.send(':exit')
Thread.kill $client.response
rescue
puts 'exiting'
end
Thread.kill $receiving_message
Gtk.main_quit()
exit
end

这将导致此输出。

app.rb:81:in `exit': exit
from app.rb:81:in `on_main_window_destroy'
from /home/user/.gem/ruby/2.4.0/gems/gobject-introspection-3.1.8/lib/gobject-introspection/loader.rb:110:in `invoke'
from /home/user/.gem/ruby/2.4.0/gems/gobject-introspection-3.1.8/lib/gobject-introspection/loader.rb:110:in `block in define_singleton_method'
from app.rb:97:in `<main>'

该程序运行良好..它不会给我造成混乱..但是我想知道这些错误的原因,以便我可以处理它。

Kernel#exit会引发异常以终止程序,这看起来像您询问的异常消息:

通过引发系统退出异常来启动 Ruby 脚本的终止。可能会捕获此异常。

至于堆栈跟踪的"block in define_singleton_method"部分,虽然 ruby 有一个define_singleton_method,如果你查看指定该文件的第 110 行,你会看到你所在的方法也被称为define_singleton_method并且你在该方法的一个块内:

def define_singleton_method(klass, name, info)
# ... 
singleton_class.__send__(:define_method, name) do |*arguments, &block|
arguments, block = prepare.call(arguments, &block) # <<< Line 110
# ...
end
end

我不确定为什么你实际上看到该输出而不是像往常那样静默退出,一种可能性是代码中的某个地方,某些东西只是在拯救基本Exception,而不是StandardError,这通常是一个坏主意,尽管他们可能只是记录/输出并重新提出它(如其中一些答案所示, 没关系),这一切都只是猜测,没有进一步挖掘代码(它甚至可能不在这个宝石中)

最新更新