Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我如何重播SIGINT?_Ruby_Signals - Fatal编程技术网

Ruby 我如何重播SIGINT?

Ruby 我如何重播SIGINT?,ruby,signals,Ruby,Signals,我目前拥有的: trap "SIGINT" do case ENV["MODE"] when "A" ... when "B" ... end end 如果我想在未设置ENV[“MODE”]时没有陷阱,我会: trap "SIGINT" do case ... end end if ENV["MODE"] 但如果我想传递一个特定值的陷阱呢 trap "SIGINT" do case ENV["MODE"] when "A" ..

我目前拥有的:

trap "SIGINT" do
  case ENV["MODE"]
  when "A"
    ...
  when "B"
    ...
  end
end
如果我想在未设置
ENV[“MODE”]
时没有陷阱,我会:

trap "SIGINT" do
  case
    ...
  end
end if ENV["MODE"]
但如果我想传递一个特定值的陷阱呢

trap "SIGINT" do
  case ENV["MODE"]
  when "A"
    ...
  when "B"
    ...
  when "C"
    # here I want to really do a SIGINT
  else
    # or here
  end
end
请 请按照@ndn的建议在
案例中定义
陷阱
如果
的话,而不是相反

取消定义陷阱
trap
注意:这只是一个概念证明

调用
Process.kill('INT',Process.pid)
内部
时,“C”
会再次陷入
陷阱。您需要先取消定义
trap
。从:

如果命令是“DEFAULT”或“SIG_DFL”,则Ruby的默认处理程序 将被调用

下面是一个例子:

trap "SIGINT" do
  mode = %w(A B C D).sample
  puts "Sigint with mode : #{mode}"
  case mode
  when "A"
    puts "A, not exiting"
  when "B"
    puts "B, not exiting"
  when "C"
    puts "C, exiting"
    trap "SIGINT", "DEFAULT"
    Process.kill('INT', Process.pid)
  else
    puts "D, exiting"
    trap "SIGINT", "DEFAULT"
    Process.kill('INT', Process.pid)
  end
end

while true
  sleep 0.1
  p "+1"
end
它输出:

"+1"
"+1"
"+1"
^CSigint with mode : A
A, not exiting
"+1"
"+1"
"+1"
"+1"
"+1"
"+1"
^CSigint with mode : C
C, exiting
trap_sigint.rb:21:in `sleep': Interrupt
    from trap_sigint.rb:21:in `<main>'
它输出

"+1"
"+1"
^CSigint with mode : A
A, not exiting
"+1"
"+1"
"+1"
"+1"
^CSigint with mode : B
B, not exiting
"+1"
"+1"
"+1"
"+1"
^CSigint with mode : D
D, exiting

预期的行为是什么?为什么不将
if-ENV['MODE']
替换为
if['A','B']。包括ENV['MODE']
?但要回答您的问题-
Process.kill('INT',Process.pid)
@EricDuminil,其工作方式类似于从
rescue之间调用
raise
。。。end
@ndn:您修改的
if
可能是最好的解决方案。我认为
Process.kill('INT',Process.pid)
会再次被陷阱抓住,不过。@EricDuminil,是的,是有道理的。
"+1"
"+1"
^CSigint with mode : A
A, not exiting
"+1"
"+1"
"+1"
"+1"
^CSigint with mode : B
B, not exiting
"+1"
"+1"
"+1"
"+1"
^CSigint with mode : D
D, exiting