Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Kernel#at_exit
() { () -> void } -> ::Proc
at_exit { block } → proc
eval_jump.c
View on GitHub ↗
static VALUE
rb_f_at_exit(VALUE _)
{
VALUE proc;
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "called without a block");
}
rb_ractor_ensure_main_ractor("can not call at_exit from non-main Ractors");
proc = rb_block_proc();
rb_set_end_proc(rb_call_end_proc, proc);
return proc;
}
Converts block to a Proc object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.
Example
def do_at_exit(str1) at_exit { print str1 } end at_exit { puts "cruel world" } do_at_exit("goodbye ") exit
produces:
Example
goodbye cruel world