From bb12b0bc6c26ccf2e91c836b6b0b6eb12ea40954 Mon Sep 17 00:00:00 2001 From: Arne Brasseur Date: Tue, 29 Apr 2014 13:36:14 +0800 Subject: [PATCH] Implement Method#curry, which delegates to to_proc.curry --- proc.c | 8 ++++++++ test/ruby/test_method.rb | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/proc.c b/proc.c index e3cecb7..bd06a4e 100644 --- a/proc.c +++ b/proc.c @@ -1804,6 +1804,13 @@ rb_method_call(int argc, VALUE *argv, VALUE method) } VALUE +rb_method_curry(int argc, VALUE *argv, VALUE self) +{ + VALUE proc = rb_funcall(self, rb_intern("to_proc"), 0); + return rb_funcall2(proc, rb_intern("curry"), argc, argv); +} + +VALUE rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval) { VALUE result = Qnil; /* OK */ @@ -2632,6 +2639,7 @@ Init_Proc(void) rb_define_method(rb_cMethod, "hash", method_hash, 0); rb_define_method(rb_cMethod, "clone", method_clone, 0); rb_define_method(rb_cMethod, "call", rb_method_call, -1); + rb_define_method(rb_cMethod, "curry", rb_method_curry, -1); rb_define_method(rb_cMethod, "[]", rb_method_call, -1); rb_define_method(rb_cMethod, "arity", method_arity_m, 0); rb_define_method(rb_cMethod, "inspect", method_inspect, 0); diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 2e3c2ae..eab0328 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -754,4 +754,13 @@ class TestMethod < Test::Unit::TestCase m = assert_nothing_raised(NameError, feature8391) {break o.singleton_method(:bar)} assert_equal(:bar, m.call, feature8391) end + + def test_curry + c = Class.new + p = proc {|a,b,c| a + b + c } + c.class_eval { define_method(:three_args, p) } + curried = c.new.method(:three_args).curry + + assert_equal(6, curried.(1).(2).(3)) + end end -- 1.9.1