aboutsummaryrefslogtreecommitdiffstats
path: root/proc.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-11-19 09:38:22 -0800
committerJeremy Evans <code@jeremyevans.net>2021-12-30 14:37:42 -0800
commitf53dfab95c30e222f67e610234f63d3e9189234d (patch)
tree83a9c1d49e3f96e36f75721d0ca1437aa2b56677 /proc.c
parent2d2ee338f3427d39d9977c77b09e5d335b6e362b (diff)
downloadruby-f53dfab95c30e222f67e610234f63d3e9189234d.tar.gz
Add support for anonymous rest and keyword rest argument forwarding
This allows for the following syntax: ```ruby def foo(*) bar(*) end def baz(**) quux(**) end ``` This is a natural addition after the introduction of anonymous block forwarding. Anonymous rest and keyword rest arguments were already supported in method parameters, this just allows them to be used as arguments to other methods. The same advantages of anonymous block forwarding apply to rest and keyword rest argument forwarding. This has some minor changes to #parameters output. Now, instead of `[:rest], [:keyrest]`, you get `[:rest, :*], [:keyrest, :**]`. These were already used for `...` forwarding, so I think it makes it more consistent to include them in other cases. If we want to use `[:rest], [:keyrest]` in both cases, that is also possible. I don't think the previous behavior of `[:rest], [:keyrest]` in the non-... case and `[:rest, :*], [:keyrest, :**]` in the ... case makes sense, but if we did want that behavior, we'll have to make more substantial changes, such as using a different ID in the ... forwarding case. Implements [Feature #18351]
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/proc.c b/proc.c
index d075b7382e..6cdd7bd836 100644
--- a/proc.c
+++ b/proc.c
@@ -3124,6 +3124,16 @@ method_inspect(VALUE method)
rb_str_buf_cat2(str, "(");
+ if (RARRAY_LEN(params) == 3 &&
+ RARRAY_AREF(RARRAY_AREF(params, 0), 0) == rest &&
+ RARRAY_AREF(RARRAY_AREF(params, 0), 1) == ID2SYM('*') &&
+ RARRAY_AREF(RARRAY_AREF(params, 1), 0) == keyrest &&
+ RARRAY_AREF(RARRAY_AREF(params, 1), 1) == ID2SYM(idPow) &&
+ RARRAY_AREF(RARRAY_AREF(params, 2), 0) == block &&
+ RARRAY_AREF(RARRAY_AREF(params, 2), 1) == ID2SYM('&')) {
+ forwarding = 1;
+ }
+
for (int i = 0; i < RARRAY_LEN(params); i++) {
pair = RARRAY_AREF(params, i);
kind = RARRAY_AREF(pair, 0);
@@ -3159,8 +3169,7 @@ method_inspect(VALUE method)
}
else if (kind == rest) {
if (name == ID2SYM('*')) {
- forwarding = 1;
- rb_str_cat_cstr(str, "...");
+ rb_str_cat_cstr(str, forwarding ? "..." : "*");
}
else {
rb_str_catf(str, "*%"PRIsVALUE, name);
@@ -3173,6 +3182,9 @@ method_inspect(VALUE method)
else if (i > 0) {
rb_str_set_len(str, RSTRING_LEN(str) - 2);
}
+ else {
+ rb_str_cat_cstr(str, "**");
+ }
}
else if (kind == block) {
if (name == ID2SYM('&')) {