Kernel.abort: Quit and Print an Error to STDERR

General 4 October 2009 | 0 Comments

One of the great things about going through other people’s Ruby books is that you discover small tidbits about the language that you’d never noticed before. While skimming through The Ruby Way again to come up with ideas for this blog, I noticed a basic program on page 14 (2nd edition) that starts like so:

print "Please enter a temperature and scale (C or F): "
str = gets
exit if str.nil? or str.empty?
str.chomp!
temp, scale = str.split(" ")

abort "#{temp} is not a valid number." if temp !~ /-?\d+/

The abort line threw me for a loop; I’d not seen it in use before. I jumped into irb to give it a try and, yes, it works and, yes, it’s documented:

abort.png

Writing to STDERR can be pretty handy, especially if you’re a UNIX nerd threading together apps at the command line. So.. a nice find, even on such an early page of the book.

Leave a Reply