简体   繁体   English

ruby at_exit 退出状态

[英]ruby at_exit exit status

Can i determine selves process exit status in at_exit block?我可以在 at_exit 块中确定自己的进程退出状态吗?

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

using idea from tadman使用tadman的想法

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

or without Perlisms:或没有 Perlisms:

require 'English'

at_exit do
  if $ERROR_INFO.nil? || ($ERROR_INFO.is_a?(SystemExit) && $ERROR_INFO.success?)
    print 'success'
  else
    code = $ERROR_INFO.is_a?(SystemExit) ? $ERROR_INFO.status : 1
    print "failure with code #{code}"
  end
end

Although the documentation on this is really thin, $!虽然这方面的文档真的很薄,但 $! is set to be the last exception that occurs, and after an exit() call this is a SystemExit exception.设置为发生的最后一个异常,并且在 exit() 调用之后,这是一个 SystemExit 异常。 Putting those two together you get this:把这两者放在一起,你会得到:

at_exit do
  if ($!.success?)
    print 'Success'
  else
    print 'Failure'
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM