Dealing with exit signals on Ruby
When working on CLIs it's useful to treat if the application exists. Image you have a long application and even if the user hits a <Ctrl+c>
to kill the process you still want to display the results so far.
There is a Ruby Kernel method at_exit that runs the block passed to it if the current process exists. Here is an example:
If you run this file and wait it until it finishes you'll get:
But now, if you run it and hit <Ctrl+c>
for finishing the process earlier you'll get:
h/t @nikkypx
Tweet