Proc#parameters: Show anonymous optionals as [:opt]

Have this for lead parameters as well as parameters after rest ("post").

[Bug #20974]
This commit is contained in:
Alan Wu 2025-01-10 12:24:39 -05:00
parent 85a25116cc
commit 6637aa4682
Notes: git 2025-01-13 17:59:18 +00:00
3 changed files with 9 additions and 4 deletions

8
iseq.c
View File

@ -3606,7 +3606,9 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
if (is_proc) {
for (i = 0; i < body->param.lead_num; i++) {
PARAM_TYPE(opt);
rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
if (rb_id2str(PARAM_ID(i))) {
rb_ary_push(a, ID2SYM(PARAM_ID(i)));
}
rb_ary_push(args, a);
}
}
@ -3631,7 +3633,9 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
if (is_proc) {
for (i = body->param.post_start; i < r; i++) {
PARAM_TYPE(opt);
rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
if (rb_id2str(PARAM_ID(i))) {
rb_ary_push(a, ID2SYM(PARAM_ID(i)));
}
rb_ary_push(args, a);
}
}

2
proc.c
View File

@ -4323,7 +4323,7 @@ proc_ruby2_keywords(VALUE procval)
*
* p = proc { it**2 }
* l = lambda { it**2 }
* p.parameters # => [[:opt, nil]]
* p.parameters # => [[:opt]]
* p.arity # => 1
* l.parameters # => [[:req]]
* l.arity # => 1

View File

@ -1382,7 +1382,8 @@ class TestProc < Test::Unit::TestCase
assert_equal([[:opt, :a], [:rest, :b], [:opt, :c]], proc {|a, *b, c|}.parameters)
assert_equal([[:opt, :a], [:rest, :b], [:opt, :c], [:block, :d]], proc {|a, *b, c, &d|}.parameters)
assert_equal([[:opt, :a], [:opt, :b], [:rest, :c], [:opt, :d], [:block, :e]], proc {|a, b=:b, *c, d, &e|}.parameters)
assert_equal([[:opt, nil], [:block, :b]], proc {|(a), &b|a}.parameters)
assert_equal([[:opt], [:block, :b]], proc {|(a), &b|a}.parameters)
assert_equal([[:opt], [:rest, :_], [:opt]], proc {|(a_), *_, (b_)|}.parameters)
assert_equal([[:opt, :a], [:opt, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:opt, :f], [:opt, :g], [:block, :h]], proc {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters)
assert_equal([[:req]], method(:putc).parameters)