ruby at_exit exit status

tig picture tig · Jul 17, 2009 · Viewed 11.9k times · Source

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

Answer

tig picture tig · Jul 20, 2009

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