class Handlers
Represents a configurable set of signal handlers.
Definitions
def initialize
Initialize the handlers.
Implementation
def initialize
@signals = {}
end
def trap(signal, &block)
Trap a signal while these handlers are installed.
Signature
-
parameter
signalSymbol | String | Integer The signal to trap.
-
yields
{|signal, context| ...} The signal number and the context that installed the handler set.
Implementation
def trap(signal, &block)
@signals[normalize(signal)] = block
end
def ignore(signal)
Ignore a signal while these handlers are installed.
Signature
-
parameter
signalSymbol | String | Integer The signal to ignore.
Implementation
def ignore(signal)
trap(signal)
end
def each(&block)
Iterate over the configured signal handlers.
Signature
-
yields
{|signal, handler| ...} The signal name and the handler, or
nilif ignored.
Implementation
def each(&block)
@signals.each(&block)
end
def normalize(signal)
Normalize signals so the controller has one portable key per OS signal.
This ensures equivalent forms like :USR1, "USR1" and "SIGUSR1" share
the same installed trap and restoration lifecycle.
Implementation
def normalize(signal)
case signal
when Integer
::Signal.list.invert.fetch(signal) do
raise ArgumentError, "unsupported signal number `#{signal}'"
end
when Symbol, String
name = signal.to_s
name = name.delete_prefix("SIG")
::Signal.list.fetch(name) do
raise ArgumentError, "unsupported signal `SIG#{name}'"
end
name
else
raise ArgumentError, "bad signal type #{signal.class}"
end
end