From: Eric Hodel Date: 2011-02-03T09:28:42+09:00 Subject: Re: How to know the exit status within at_exit() block? On Feb 2, 2011, at 02:46, Iņaki Baz Castillo wrote: > 2011/2/1 Eric Hodel : >>> Hi, my program invokes "exit true" or "exit false" and I want to catch >>> such return code into at_exit() block, but I don't know how to do >>> that. >> >> ruby -e 'at_exit { p $!.status }; exit 1' > > 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 is irb's fault: $ cat t.rb def x at_exit { p $!.status } end def y exit 2 end x y $ ruby t.rb 2