forked from marcandre/backports
/
proc.rb
51 lines (46 loc) · 1.42 KB
/
proc.rb
1
2
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
unless Proc.method_defined? :lambda?
class Proc
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
def lambda?
!!@is_lambda
end
end
class Method
def to_proc_with_lambda_tracking
proc = to_proc_without_lambda_tracking
proc.instance_variable_set :@is_lambda, true
proc
end
Backports.alias_method_chain self, :to_proc, :lambda_tracking
end
module Kernel
def lambda_with_lambda_tracking(&block)
Backports.track_lambda block, lambda_without_lambda_tracking(&block), true
end
def proc_with_lambda_tracking(&block)
Backports.track_lambda block, proc_without_lambda_tracking(&block)
end
Backports.alias_method_chain self, :lambda, :lambda_tracking
Backports.alias_method_chain self, :proc, :lambda_tracking
end
end
class Proc
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
def curry(argc = nil)
min_argc = arity < 0 ? -arity - 1 : arity
argc ||= min_argc
if lambda? and arity < 0 ? -argc - 1 > arity : argc != arity
raise ArgumentError, "wrong number of arguments (#{argc} for #{min_argc})"
end
block = proc do |*args|
if args.count >= argc
call(*args)
else
proc do |*more_args|
args += more_args
block.call(*args)
end
end
end
end unless method_defined? :curry
end