Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 27, 2025 01:33
Show Gist options
  • Select an option

  • Save havenwood/5fb551ccf2795e21582f95d32ffc0995 to your computer and use it in GitHub Desktop.

Select an option

Save havenwood/5fb551ccf2795e21582f95d32ffc0995 to your computer and use it in GitHub Desktop.
at_exit with priorities
module Curtain
@hooks = []
@order = 0
@mutex = Mutex.new
class << self
attr_reader :order
def register(group:, priority: 0, &block)
@mutex.synchronize do
@hooks << {priority:, group:, order:, block:}
@order += 1
end
end
def call
@mutex.synchronize do
@hooks.sort_by! { |hook| hook.values_at(:priority, :group, :order) }
@hooks.each { |hook| hook[:block].call }
end
end
end
at_exit { Curtain.call }
end
module Kernel
def at_exit(...) = Curtain.register(...)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment