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
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
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
at_exit
for this is ok for small scripts. For larger apps, you should be using exit
(or calling your own method to handle exits) at the intended exit points, and handling errors, exceptions, and signals explicitly. –
Embarrassment ($!.is_a?(SystemExit) && $!.success?)
? –
Leastwise exit
(or raise SystemExit
) and exit code is successful (equal to 0
) –
Oppidan status
, so you can $!.status.zero?
(or even monkey-patch to_i
alias), but you still need to check if it is a SystemExit
first. –
Oppidan 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
© 2022 - 2024 — McMap. All rights reserved.