Is there a Perl equivalent END block in Ruby? In Perl, if I specify an END block, the code in that block will get executed no matter where the program bails out. It is great functionality for closing open file handles. Does Ruby support similar functionality? I tried Ruby’s “END{}” block but that doesnt seem to get called if I had an exit in the code due to an error.
Thanks!
Use
at_exit, which will run regardless of whether an exception was raised or not:prints “exited” as expected.
You should only consider this if you cannot use an
ensure, asat_exitcauses logic to reside far away from where the actual exit occurs.