The following code surprised me:
The result of this code is:
Like all things in ruby, there is a way to bypass this:
The result of this code is:
One of the interesting side-effects of processing ensure blocks is that we can actually cancel the exit function:
The surprising result of this program is:
Therefore, even though we asked ruby to exit
, because of the second exception, the program kept running! The reason for this is that exit
doesn't actually do what it says it does; actually it simply raises the SystemExit
exception, which when received at the top level, causes the interpreter to stop.
The result of this code is:
This is by design: It means that you can process exit in a way other than exiting a program. For example, in a networking library, you might not want individual connections exiting the entire app, so you can can trap this kind of behaviour and deal with it specifically.
Comments
Leave a comment
Please note, comments must be formatted using Markdown. Links can be enclosed in angle brackets, e.g.
<www.codeotaku.com>
.