Tuesday, February 05, 2013

Ruby: Ensure blocks can alter flow control!

This one bit me -- I've only found it documented in one place, here, but I also discovered the behavior after debugging a strange problem I was having with some command line processes not exiting properly when they received the exit command.

Basically, if you exit from an ensure block with "return" it behaves the same as if you had done a "rescue Exception" statement, which means it swallows up all exceptions, including exceptions raised to terminate the Ruby process when it receives SIGTERM, SIGQUIT, SIGINT, etc.

No comments: