Sam Elliott home

Plink!

18 Apr 2010

I was helping out with an old rails ticket, and thought I'd share a code snippet i worked on when solving it.

The ticket was to do with someone who thought it would be good to get the terminal play its alert sound (known as an audible bell, as opposed to a visual one) whenever a rake task failed because of an issue with pending migrations.

Thankfully, how rails 3 has built its rake tasks, this is a relatively easy addition. The task db:abort_if_pending_migrations contains the logic you need to mess with, but it's not so easy. The method abort is called, which automatically exits, so we can't append any logic to the rake task.

Thankfully, Ruby is awesome, and Rake is flexible. By redefining or monkey-patching the abort method, we can get it to make a sound. Here is the snippet:

For your information, \a is the escape code for sending an audible bell to the terminal.

Fork me on GitHub