On Wed, Feb 2, 2011 at 4:46 AM, I=F1aki Baz Castillo <ibc / aliax.net> wrote:

> 2011/2/1 Eric Hodel <drbrain / segment7.net>:
> >> 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 =3D #{$!.inspect}" }
> #<Proc:0x000000019f99c0@(irb):1>
> irb> exit 1
> "$!.inspect =3D nil"
>
> So $! is nil and I cannot get $!.status.
>
>
> --
> I=F1aki Baz Castillo
> <ibc / aliax.net>
>
>
It probably doesn't work in IRB, because IRB wraps calls to exit

irb> method :exit
=3D> #<Method: Object(IRB::ExtendCommandBundle)#exit>

I think that using $! is probably what you should do.