From 75ecbda438670ec12641d1324d0e81a52ee02e0a Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 27 Oct 2021 10:35:54 -0700 Subject: Make Module#{public,private,protected,module_function} return arguments Previously, each of these methods returned self, but it is more useful to return arguments, to allow for simpler method decorators, such as: ```ruby cached private def foo; some_long_calculation; end ``` Where cached sets up caching for the method. For each of these methods, the following behavior is used: 1) No arguments returns nil 2) Single argument is returned 3) Multiple arguments are returned as an array The single argument case is really the case we are trying to optimize for, for the same reason that def was changed to return a symbol for the method. Idea and initial patch from Herwin Quarantainenet. Implements [Feature #12495] --- vm_method.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'vm_method.c') diff --git a/vm_method.c b/vm_method.c index 337d326092..9d0c59c01e 100644 --- a/vm_method.c +++ b/vm_method.c @@ -2118,11 +2118,14 @@ set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t if (argc == 0) { scope_visibility_check(); rb_scope_visibility_set(visi); + return Qnil; } - else { - set_method_visibility(module, argc, argv, visi); + + set_method_visibility(module, argc, argv, visi); + if (argc == 1) { + return argv[0]; } - return module; + return rb_ary_new_from_values(argc, argv); } /* @@ -2469,7 +2472,7 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module) if (argc == 0) { rb_scope_module_func_set(); - return module; + return Qnil; } set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE); @@ -2495,7 +2498,10 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module) } rb_method_entry_set(rb_singleton_class(module), id, me, METHOD_VISI_PUBLIC); } - return module; + if (argc == 1) { + return argv[0]; + } + return rb_ary_new_from_values(argc, argv); } #ifdef __GNUC__ -- cgit v1.2.1