Module: Tins::PartialApplication

Defined in:
lib/tins/partial_application.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(modul) ⇒ Object

If this module is included into a Proc (or similar object), it tampers with its Proc#arity method.



5
6
7
8
9
10
11
12
13
# File 'lib/tins/partial_application.rb', line 5

def self.included(modul)
  modul.module_eval do
    old_arity = instance_method(:arity)
    define_method(:arity) do
      defined?(@__arity__) or old_arity.bind(self).call
    end
  end
  super
end

Instance Method Details

#partial(*args) ⇒ Object

Create a partial application of this Proc (or similar object) using args as the already applied arguments.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tins/partial_application.rb', line 17

def partial(*args)
  if args.empty?
    dup
  elsif args.size > arity
    raise ArgumentError, "wrong number of arguments (#{args.size} for #{arity})"
  else
    f = lambda { |*b| call(*(args + b)) }
    f.instance_variable_set :@__arity__, arity - args.size
    f
  end
end