Skip to content
Browse files

Isolate arity and args from successive calls to Proc#curry.

Fixes #1523.
  • Loading branch information...
1 parent 9cb8243 commit 72703450108175e45fb4b21fb7d07676e001c952 @headius headius committed Feb 22, 2014
View
31 core/src/main/ruby/jruby/kernel19/proc.rb
@@ -16,19 +16,7 @@ def curry(curried_arity = nil)
end
end
- args = []
-
- my_self = self
- m = lambda? ? :lambda : :proc
- f = __send__(m) {|*x|
- call_args = args + x
- if call_args.length >= my_self.arity
- my_self[*call_args]
- else
- args = call_args
- f
- end
- }
+ f = Proc.__make_curry_proc__(self, [], arity)
f.singleton_class.send(:define_method, :binding) {
raise ArgumentError, "cannot create binding from f proc"
@@ -44,4 +32,21 @@ def curry(curried_arity = nil)
f
end
+
+ def self.__make_curry_proc__(proc, passed, arity)
+ is_lambda = proc.lambda?
+ passed.freeze
+
+ __send__((is_lambda ? :lambda : :proc)) do |*argv, &passed_proc|
+ my_passed = passed + argv
+ if my_passed.length < arity
+ if !passed_proc.nil?
+ warn "#{caller[0]}: given block not used"
+ end
+ __make_curry_proc__(proc, my_passed, arity)
+ else
+ proc.call(*my_passed)
+ end
+ end
+ end
end
View
13 spec/regression/GH-1523_proc_curry_should_not_produce_arity_errors_spec.rb
@@ -0,0 +1,13 @@
+describe "Proc#curry" do
+ describe "when applied successively" do
+ it "combines arguments and calculates incoming arity accurately" do
+ l = lambda{|a,b,c| a+b+c }
+ l1 = l.curry.call(1)
+ # the l1 currying seems unnecessary, but it triggered the original issue
+ l2 = l1.curry.call(2)
+
+ expect(l2.curry.call(3)).to eq 6
+ expect(l1.curry.call(2,3)).to eq 6
+ end
+ end
+end

0 comments on commit 7270345

Please sign in to comment.
Something went wrong with that request. Please try again.