Ruby $!
This week I enjoyed using Ruby's built-in global variables, including $!
. $!
refers to the last error that was raised, and is useful in lots of different contexts such as Rake tasks.
Here it is in action:
:001 > begin; raise NameError; rescue; puts "The error we raised was #$!."; end
The error we raised was NameError.
And a list of these global variables:
https://github.com/ruby/ruby/blob/trunk/lib/English.rb
For a slightly less bare-metal implementation, require this library and utitlize the friendly names:
:001 > require 'english'
=> true
:002 > begin; raise NameError; rescue; puts "The error we raised was #$ERROR_INFO."; end
The error we raised was NameError.
Tweet