15

at_exit ブロックでプロセスの終了ステータスを自分で判断できますか?

at_exit do
  if this_process_status.success?
    print 'Success'
  else
    print 'Failure'
  end
end
4

2 に答える 2

21

タッドマンのアイデアを使用

at_exit do
  if $!.nil? || ($!.is_a?(SystemExit) && $!.success?)
    print 'success'
  else
    code = $!.is_a?(SystemExit) ? $!.status : 1
    print "failure with code #{code}"
  end
end
于 2009-07-20T16:50:52.980 に答える
19

これに関するドキュメントは本当に薄いですが、$!が発生する最後の例外として設定され、exit()呼び出しの後、これはSystemExit例外です。これら2つを組み合わせると、次のようになります。

at_exit do
  if ($!.success?)
    print 'Success'
  else
    print 'Failure'
  end
end
于 2009-07-17T20:30:40.520 に答える