gpt4 book ai didi

Ruby:从陷阱上下文运行 irb?

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:40 33 4
gpt4 key购买 nike

Programming Ruby 1.9 & 2.0 一书中给出了以下示例:

require 'irb'

trap "INT" do
IRB.start
end

count = 0
loop do
count += 1
puts count
puts "Value = #{@value}" if defined? @value
sleep 1
end

不幸的是,如果我使用我的 Ruby 2.5.1 设置运行此代码,我会得到以下 ThreadError(run_irb.rb 是包含上述代码的文件的名称):

    24: from run_irb.rb:8:in `<main>'
23: from run_irb.rb:8:in `loop'
22: from run_irb.rb:12:in `block in <main>'
21: from run_irb.rb:12:in `sleep'
20: from run_irb.rb:4:in `block in <main>'
19: from /usr/lib/ruby/2.5.0/irb.rb:376:in `start'
18: from /usr/lib/ruby/2.5.0/irb/init.rb:17:in `setup'
17: from /usr/lib/ruby/2.5.0/irb/init.rb:112:in `init_config'
16: from /usr/lib/ruby/2.5.0/irb/init.rb:112:in `new'
15: from /usr/lib/ruby/2.5.0/irb/locale.rb:32:in `initialize'
14: from /usr/lib/ruby/2.5.0/irb/locale.rb:108:in `load'
13: from /usr/lib/ruby/2.5.0/irb/locale.rb:124:in `find'
12: from /usr/lib/ruby/2.5.0/irb/locale.rb:145:in `search_file'
11: from /usr/lib/ruby/2.5.0/irb/locale.rb:157:in `each_localized_path'
10: from /usr/lib/ruby/2.5.0/irb/locale.rb:167:in `each_sublocale'
9: from /usr/lib/ruby/2.5.0/irb/locale.rb:158:in `block in each_localized_path'
8: from /usr/lib/ruby/2.5.0/irb/locale.rb:150:in `block in search_file'
7: from /usr/lib/ruby/2.5.0/rubygems.rb:213:in `try_activate'
6: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `find_by_path'
5: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `find'
4: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1063:in `each'
3: from /usr/lib/ruby/2.5.0/rubygems/specification.rb:1064:in `block in find_by_path'
2: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:40:in `require'
1: from /usr/lib/ruby/2.5.0/monitor.rb:185:in `mon_enter'
/usr/lib/ruby/2.5.0/monitor.rb:185:in `lock': can't be called from trap context (ThreadError)

在 Ruby 2.5.1 中有没有办法让 irb 从陷阱上下文中运行?

最佳答案

Ruby 2.0 does not allow Mutex#lock within a signal handler

根据这个bug 7917在 ruby​​ lang 中,这是“预期的”行为。所以你基本上发现了一个语言错误和一个书籍错误,因为作者没有在 ruby​​ 2.0 中测试这个例子(确认它在 1.9 中工作。)

另一种方法是使用异常/救援并从中断中执行 IRB session ,如下所示:

require 'irb'

count = 0
loop do
count += 1
puts count
puts "Value = #{@value}" if defined? @value
sleep 1
rescue Interrupt => e
IRB.start
break
end

以上代码将正确捕获中断并允许启动 IRB session 。我确实注意到 IRB.start 不会给你一个本地执行上下文,所以你可能想看看 binding.irb 让你开始一个更有用的调试 session 像这样:

$ ruby thread-test.rb
1
2
^C
[1] irb(main)>

From: /work/_scratch/thread-test/thread-test.rb @ line 11 :

6: puts count
7: puts "Value = #{@value}" if defined? @value
8: sleep 1
9: rescue Interrupt => e
10: binding.irb
=> 11: break
12: end

[2] irb(main)> e
=> Interrupt
[3] irb(main)> count
=> 2

我找到了一些关于处理 Signals, Traps, and Rescues 的博文和 Graceful Shutdown这可能会帮助您进一步解决问题。

关于Ruby:从陷阱上下文运行 irb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53399991/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com