Just Abort It
Posted: August 24th, 2009 | Author: Jerod | Filed under: Ruby | View CommentsA lot of people end up writing Ruby methods that looks something like this:
def stop_error(message) puts "ERROR: #{message}" exit(1) end
Which they call in their app like so:
stop_error "Oh noes, file doesn't exist!" unless File.exist?(file)
I used to write that method a lot too. Did you know Ruby has a built-in method that provides just what we’re all looking for?
Kernel::abort
So, stop writing your own little method and just abort it:
abort "Oh noes, file doesn't exist!" unless File.exist?(file)
And of course abort follows conventions by writing the error message on STDERR, so that's one less thing to think about.
Good point, another benefit!
And of course abort follows conventions by writing the error message on STDERR, so that's one less thing to think about.
Good point, another benefit!