Sha256: 0968cf6f0aeb0a45802db3148ac7f55a835497c6268109d00de0409f555e8118

Contents?: true

Size: 859 Bytes

Versions: 3

Compression:

Stored size: 859 Bytes

Contents

module Kernel
  # Calling Kernel#trap() by itself will replace any previously registered
  # handler code. Kernel#trap_chain(), on the other hand, will add the block
  # you supply to the existing "list" of registered handler blocks. Similar
  # to the way Kernel#at_exit() works, Kernel#trap_chain() will prepend the
  # given block to the call chain for the given signal_name. When the signal
  # occurs, your block will be executed first and then the previously
  # registered handler will be invoked. This can be called repeatedly
  # to create a "chain" of handlers. 
  #
  # CREDIT: Tyler Rick
  def trap_chain(signal_name, *args, &block)
    previous_interrupt_handler = trap(signal_name, *args) {}
    trap(signal_name, *args) do
      block.call
      previous_interrupt_handler.call unless previous_interrupt_handler == "DEFAULT"
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.9.0 lib/tour/facets/kernel/trap_chain.rb
facets-2.9.0.pre.2 lib/tour/facets/kernel/trap_chain.rb
facets-2.9.0.pre.1 lib/tour/facets/kernel/trap_chain.rb