From 6637aa4682ef64134e05af949a9beee260dab937 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Fri, 10 Jan 2025 12:24:39 -0500 Subject: [PATCH] Proc#parameters: Show anonymous optionals as `[:opt]` Have this for lead parameters as well as parameters after rest ("post"). [Bug #20974] --- iseq.c | 8 ++++++-- proc.c | 2 +- test/ruby/test_proc.rb | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/iseq.c b/iseq.c index df943b8943..614d4a0f87 100644 --- a/iseq.c +++ b/iseq.c @@ -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); } } diff --git a/proc.c b/proc.c index 0608f584c8..88e0fbebbb 100644 --- a/proc.c +++ b/proc.c @@ -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 diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 966144c199..3c3d83a144 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -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)