Project

General

Profile

Actions

Bug #18011

closed

`Method#parameters` is incorrect for forwarded arguments

Added by josh.cheek (Josh Cheek) 4 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [arm64-darwin20]
[ruby-core:104430]

Description

When asking a method about its parameters, forwarded arguments say they are a rest and a block (wrapper1 in the example below).
However, when we use that signature, it raises an ArgumentError (wrapper2 in the example below).
When I add a keyrest to the signature, it works as expected (wrapper3 in the example below).
So I think forwarded arguments should include a keyrest in the output of Method#parameters

def wrapped(ord, kw:) = [ord, {kw: kw}]

methods = [
 def wrapper1(...)         = wrapped(...),
 def wrapper2(*r, &b)      = wrapped(*r, &b),
 def wrapper3(*r, **k, &b) = wrapped(*r, **k, &b),
]

methods.each do |name|
  puts File.read(__FILE__)[/#{name}\(.*?\)/]
  puts "  params: #{method(name).parameters.inspect}"
  puts "  result: #{(method(name).call 123, kw: 456 rescue $!).inspect}"
  puts
end

Output:

wrapper1(...)
  params: [[:rest, :*], [:block, :&]]
  result: [123, {:kw=>456}]

wrapper2(*r, &b)
  params: [[:rest, :r], [:block, :b]]
  result: #<ArgumentError: wrong number of arguments (given 2, expected 1; required keyword: kw)>

wrapper3(*r, **k, &b)
  params: [[:rest, :r], [:keyrest, :k], [:block, :b]]
  result: [123, {:kw=>456}]
Actions

Also available in: Atom PDF