rubyexitstatusatexit

ruby at_exit exit status


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

Solution

  • 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
    

    or without 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