views:

1109

answers:

2

Can i determine selves process exit status in at_exit block?

at_exit do
  if this_process_status.success?
    print 'Success'
  else
    print 'Failure'
  end
end
+2  A: 

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. Putting those two together you get this:

at_exit do
  if ($!.success?)
    print 'Success'
  else
    print 'Failure'
  end
end
tadman
That will be the case only if exit will be called.Surely I can be test if $! is nil or is SystemExit which answers true on success?But is it possible to get Process::Status object or it is not created for the top process?
tig
using your idea I got answer and posted but I don't know which answer is better to mark - yours or mine?
tig
Whatever works for you. Didn't know you could "answer" your own question, but okay.
tadman
A: 

using idea from 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
tig
And what? After finding full answer, I decided to share it.
tig
just my opinion would be to give the "check" to tadman instead of yourself /shrug.
Reed Debaets
I think that the fullest answer should be checked. Check is most useful for other users looking for answer and I don't think that anything will change for me and tadman, whatever answer I will check.
tig