From: "zverok (Victor Shepelev) via ruby-core" Date: 2023-10-24T20:53:06+00:00 Subject: [ruby-core:115157] [Ruby master Misc#19971] Confusing arity of a Proc with implicit rest parameter Issue #19971 has been updated by zverok (Victor Shepelev). @shan I don't think this documentation definition covers the case described by OP. `proc { |a,| }` is not the same as `proc { |a| }`, and is semantically equivalent of `proc { |a,*| }`. I think it is reasonable to expect its `arity`���and, by the way, `parameters`, would behave as in latter, not as in former. I think this would be reasonable (and might be useful for metaprogramming code that checks the signature of the passed callable object): ```ruby proc { |a| a }.call([1, 2, 3]) #=> [1, 2, 3] proc { |a| }.arity #=> 1 proc { |a| }.parameters #=> [[:opt, :a]] proc { |a, *| a }.call([1, 2, 3]) #=> 1 proc { |a, *| }.arity #=> -2 proc { |a, *| }.parameters #=> [[:opt, :a], [:rest, :*]] proc { |a,| a }.call([1, 2, 3]) #=> 1, like the latter proc { |a,| }.arity #=> 1, should be -2 proc { |a,| }.parameters #=> [[:opt, :a]], should be [[:opt, :a], [:rest, :*]] ``` Though, experimenting a bit more with it, there is a funny edge case: ```ruby define_method(:m1, &proc { |a| }) define_method(:m2, &proc { |a, *| }) define_method(:m3, &proc { |a,| }) p method(:m1) # p method(:m2) # p method(:m3) # --- like 1, not like 2 ``` ...which is probably related to reported arity/parameters. ---------------------------------------- Misc #19971: Confusing arity of a Proc with implicit rest parameter https://bugs.ruby-lang.org/issues/19971#change-105064 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal ---------------------------------------- I've noticed that such proc `proc { |a,| }` has arity `1` that means 1 required parameter, but behaves like there is a rest parameter (`proc { |a, *| }`) that has arity `-2` (that means 1 required parameter + rest parameter). So I am wondering whether it's intentional behaviour and what the rational is behind it. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/