Module: Multitrap::PatchedTrap

Defined in:
lib/multitrap/patched_trap.rb

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/multitrap/patched_trap.rb', line 3

def self.included(mod)
  original_trap = Signal.method(:trap)

  mod.instance_eval do
    define_method(:trap) do |*args, &block|
      if args.size < 1
        if Multitrap.rbx?
          raise ArgumentError, "method 'trap': given 0, expected 2"
        elsif Multitrap.jruby?
          raise ArgumentError, "wrong number of arguments (0 for 1)"
        else
          raise ArgumentError, "wrong number of arguments (0 for 1..2)"
        end
      end

      if block.nil?
        if Multitrap.rbx? && !args[1].respond_to?(:call)
          raise ArgumentError, "Handler must respond to #call (was #{args[1].class})"
        elsif args[1].nil?
          raise ArgumentError, 'tried to create Proc object without a block'
        end
      end

      Multitrap::Trap.trap(original_trap, *args, &block)
    end
  end
end