Ruby: 运行 来自陷阱上下文的 irb?

Ruby: run irb from trap context?

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

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

另一种方法是使用 exception/rescue 并从这样的中断执行 IRB 会话:

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 会话启动。我确实注意到 IRB.start 不会给你一个本地执行上下文,所以你可能想看看 binding.irb 让你开始一个更有用的调试会话,像这样:

$ 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 and Graceful Shutdown 的博客文章,它们可能会帮助您进一步解决您的问题。