From a076db49b025bc9760c261400f77f09cd3bfd018 Mon Sep 17 00:00:00 2001 From: Paul Mucur Date: Tue, 23 Jun 2015 15:35:55 +0100 Subject: [PATCH 3/3] proc.c: Support any callable when composing Procs * proc.c (proc_compose): support any object with a call method rather than supporting only procs. [Feature #6284] * proc.c (compose): use the function call on the given object rather than rb_proc_call_with_block in order to support any object. * test/ruby/test_proc.rb: Add test cases for composing Procs with callable objects. * test/ruby/test_method.rb: Add test cases for composing Methods with callable objects. --- proc.c | 16 +++------------- test/ruby/test_method.rb | 20 +++++++++++++++++--- test/ruby/test_proc.rb | 17 ++++++++++++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/proc.c b/proc.c index 9f68829..1fef6e5 100644 --- a/proc.c +++ b/proc.c @@ -2824,7 +2824,7 @@ compose(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc) VALUE f, g, fargs; f = RARRAY_AREF(args, 0); g = RARRAY_AREF(args, 1); - fargs = rb_ary_new3(1, rb_proc_call_with_block(g, argc, argv, passed_proc)); + fargs = rb_ary_new3(1, rb_funcall_with_block(g, idCall, argc, argv, passed_proc)); return rb_proc_call(f, fargs); } @@ -2833,7 +2833,7 @@ compose(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc) * call-seq: * prc * g -> a_proc * - * Returns a proc that is the composition of this proc and the given proc g. + * Returns a proc that is the composition of this proc and the given g. * The returned proc takes a variable number of arguments, calls g with them * then calls this proc with the result. * @@ -2849,16 +2849,6 @@ proc_compose(VALUE self, VALUE g) rb_proc_t *procp; int is_lambda; - if (!rb_obj_is_method(g) && !rb_obj_is_proc(g)) { - rb_raise(rb_eTypeError, - "wrong argument type %s (expected Proc/Method)", - rb_obj_classname(g)); - } - - if (rb_obj_is_method(g)) { - g = method_to_proc(g); - } - args = rb_ary_new3(2, self, g); GetProcPtr(self, procp); @@ -2875,7 +2865,7 @@ proc_compose(VALUE self, VALUE g) * call-seq: * meth * g -> a_proc * - * Returns a proc that is the composition of this method and the given proc g. + * Returns a proc that is the composition of this method and the given g. * The returned proc takes a variable number of arguments, calls g with them * then calls this method with the result. * diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 739dbbb..55ed094 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -977,14 +977,28 @@ def f(x) x * 2 end assert_equal(6, h.call(2)) end - def test_compose_with_nonproc_or_method + def test_compose_with_callable + c = Class.new { + def f(x) x * 2 end + } + c2 = Class.new { + def call(x) x + 1 end + } + f = c.new.method(:f) + g = f * c2.new + + assert_equal(6, g.call(2)) + end + + def test_compose_with_noncallable c = Class.new { def f(x) x * 2 end } f = c.new.method(:f) + g = f * 5 - assert_raise(TypeError) { - f * 5 + assert_raise(NoMethodError) { + g.call(2) } end end diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index a6e9bcc..3531d55 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -1385,11 +1385,22 @@ def g(x) x + 1 end assert_equal(6, h.call(2)) end - def test_compose_with_nonproc_or_method + def test_compose_with_callable f = proc{|x| x * 2} + c = Class.new { + def call(x) x + 1 end + } + g = f * c.new + + assert_equal(6, g.call(2)) + end + + def test_compose_with_noncallable + f = proc{|x| x * 2} + g = f * 5 - assert_raise(TypeError) { - f * 5 + assert_raise(NoMethodError) { + g.call(2) } end end -- 2.6.4