From 4eaf22cc3f6639003ed3b64c3fee82c5867d27ea Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 22 Nov 2018 05:51:41 +0000 Subject: proc.c: Implement Method#* for Method composition * proc.c (rb_method_compose): Implement Method#* for Method composition, which delegates to Proc#*. * test/ruby/test_method.rb: Add test cases for Method composition. From: Paul Mucur git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65912 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- proc.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'proc.c') diff --git a/proc.c b/proc.c index efff796499..3c3544ecf6 100644 --- a/proc.c +++ b/proc.c @@ -3099,6 +3099,30 @@ proc_compose(VALUE self, VALUE g) return proc; } +/* + * call-seq: + * meth * g -> a_proc + * + * Returns a proc that is the composition of this method and the given proc g. + * The returned proc takes a variable number of arguments, calls g with them + * then calls this method with the result. + * + * def f(x) + * x * 2 + * end + * + * f = self.method(:f) + * g = proc {|x, y| x + y } + * h = f * g + * p h.call(1, 2) #=> 6 + */ +static VALUE +rb_method_compose(VALUE self, VALUE g) +{ + VALUE proc = method_to_proc(self); + return proc_compose(proc, g); +} + /* * Document-class: LocalJumpError * @@ -3222,6 +3246,7 @@ Init_Proc(void) rb_define_method(rb_cMethod, "call", rb_method_call, -1); rb_define_method(rb_cMethod, "===", rb_method_call, -1); rb_define_method(rb_cMethod, "curry", rb_method_curry, -1); + rb_define_method(rb_cMethod, "*", rb_method_compose, 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); -- cgit v1.2.3