Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Kernel#at_exit

() { () -> void } -> ::Proc
at_exit { block } → 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
See also #Array #autoload #abort #autoload? also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0