Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 1014 Bytes

exit-a-process-with-an-error-message.md

File metadata and controls

34 lines (26 loc) · 1014 Bytes

Exit A Process With An Error Message

If you want to exit a Ruby process with an unsuccessful error code like 1, you can use Kernel.exit.

if arg_is_missing?
  exit 1
end

The non-zero status code means this will work great in a scripting scenario, like ruby script.rb && echo 'success'. If the script has to exit early, it won't print 'success'.

That's fine, but it doesn't give the program a chance to tell us why the program exited early.

Kernel has another method abort that does the same thing as exit 1 while also printing a message to STDERR.

if arg_is_missing?
  abort 'The missing argument must be supplied'
end

This is handy if you want to communicate more than just the error code. The program still exits early with an error code of 1. And it prints that message to STDERR.

source