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.
I would like something as:
at_exit(exit_status) {
puts "exiting with status #{status}"
}
if SOMETHING
exit true
else
exit false
end
Of course I call "exit" from lof of clases/modules under my project so
I cannot use a local scope variable "status" (and I wouldn't like to
use a global variable).
How about stick your own exit method before Kernel in the lookup chain
class Object
private
def exit(status=true)
at_exit {
puts "exiting with status #{status}"
}
super
end
end
···
On Mon, Jan 31, 2011 at 5:09 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
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.
I would like something as:
at_exit(exit_status) {
puts "exiting with status #{status}"
}
if SOMETHING
exit true
else
exit false
end
Of course I call "exit" from lof of clases/modules under my project so
I cannot use a local scope variable "status" (and I wouldn't like to
use a global variable).
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.
I would like something as:
at_exit(exit_status) {
puts "exiting with status #{status}"
}
if SOMETHING
exit true
else
exit false
end
You are aware that you can simplify that to
exit SOMETHING
are you?
Of course I call "exit" from lof of clases/modules under my project so
I cannot use a local scope variable "status" (and I wouldn't like to
use a global variable).
First of all: IMHO that is bad practice because it prevents your code
to be used in different situations (i.e. reduces modularity). The
proper way would be to raise exceptions and deal with them
appropriately. You can even omit the handling on top level and get
appropriate exit codes of the process (i.e. 0 for the regular case, 1
for error):
However, you can do what you want with a rescue clause:
begin
# main code
rescue SystemExit => e
puts "exiting with status #{e.status}"
raise
rescue Exception => e
puts "exiting with status 1"
raise
else
puts "exiting with status 0"
end
Kind regards
robert
···
On Tue, Feb 1, 2011 at 12:09 AM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
Yes, but I want to inspect the exit status code into the block
provided to at_exit().
Of course I call "exit" from lof of clases/modules under my project so
I cannot use a local scope variable "status" (and I wouldn't like to
use a global variable).
First of all: IMHO that is bad practice because it prevents your code
to be used in different situations (i.e. reduces modularity). The
proper way would be to raise exceptions and deal with them
appropriately. You can even omit the handling on top level and get
appropriate exit codes of the process (i.e. 0 for the regular case, 1
for error):
However, you can do what you want with a rescue clause:
begin
# main code
rescue SystemExit => e
puts "exiting with status #{e.status}"
raise
rescue Exception => e
puts "exiting with status 1"
raise
else
puts "exiting with status 0"
end
Using "rescue SystemExit => e" seems indeed appropriate. Let me play a
bit with it.
Thanks a lot.
···
2011/2/1 Robert Klemme <shortcutter@googlemail.com>:
>> 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:
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:
···
2011/2/2 Josh Cheek <josh.cheek@gmail.com>:
Really interesting. However, it fails if exit() is called outside of the
scope:
On Feb 2, 2011, at 06:34, Iñaki Baz Castillo wrote:
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.