summaryrefslogtreecommitdiff
path: root/vm_method.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-10-27 10:35:54 -0700
committerJeremy Evans <code@jeremyevans.net>2021-11-18 09:47:40 -0800
commit75ecbda438670ec12641d1324d0e81a52ee02e0a (patch)
tree6ca80769c4b2ebdcababc30759ae7872275c19b8 /vm_method.c
parentec574ab3453709490b53b5cc761ec158103fe42a (diff)
downloadruby-75ecbda438670ec12641d1324d0e81a52ee02e0a.tar.gz
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]
Diffstat (limited to 'vm_method.c')
-rw-r--r--vm_method.c16
1 files changed, 11 insertions, 5 deletions
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__