From: "Iñaki Baz Castillo" Date: 2011-02-02T23:34:30+09:00 Subject: Re: How to know the exit status within at_exit() block? 2011/2/2 Josh Cheek : >> Really interesting. However, it fails if exit() is called outside of the >> scope: >> >> irb> at_exit { p "$!.inspect = #{$!.inspect}" } >> # >> irb> exit 1 >> "$!.inspect = nil" >> >> So $! is nil and I cannot get $!.status. > It probably doesn't work in IRB, because IRB wraps calls to exit > > irb> method :exit > => # > > I think that using $! is probably what you should do. You are right, however I've realized that in case of exiting without invoking "exit" or "exit!" (i.e. exiting due to a signal) then $! is not a SystemExit instance, but an Interrupt instance (and it doesn't have "status" method). For example: -------------------- at_exit { p "$!.inspect = #{$!.inspect}" } sleep 10 -------------------- If you run this program and press Ctrl+C prior to 10 seconds you get: "$!.inspect = Interrupt" So, at_exit block is executed but $! is not a SystemExit instance. I could trap the signals however. Thanks. -- Iñaki Baz Castillo