Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created June 28, 2021 22:15
Show Gist options
  • Save JoshCheek/9fde9c25492c77f69798077d5a0e0428 to your computer and use it in GitHub Desktop.
Save JoshCheek/9fde9c25492c77f69798077d5a0e0428 to your computer and use it in GitHub Desktop.
Ruby's three dot arguments misreport their parameter types
p RUBY_VERSION: RUBY_VERSION
def ab(a, b:)
[a, {b: b}]
end
puts "Three dots:"
def dots(...) ab(...) end
pp real_sig: method(:ab).parameters,
wrapper_sig: method(:dots).parameters,
result: dots(123, b: 456)
puts "", "Explicitly reproducing the alleged signature"
def alleged(*rest, &block) ab(*rest, &block) end
pp real_sig: method(:ab).parameters,
wrapper_sig: method(:alleged).parameters,
result: (alleged(123, b: 456) rescue $!)
puts "", "What I think the sigature actually is"
def actual(*rest, **kwrest, &block) ab(*rest, **kwrest, &block) end
pp real_sig: method(:ab).parameters,
wrapper_sig: method(:actual).parameters,
result: actual(123, b: 456)
__END__
Output:
{:RUBY_VERSION=>"3.0.1"}
Three dots:
{:real_sig=>[[:req, :a], [:keyreq, :b]],
:wrapper_sig=>[[:rest, :*], [:block, :&]],
:result=>[123, {:b=>456}]}
Explicitly reproducing the alleged signature
{:real_sig=>[[:req, :a], [:keyreq, :b]],
:wrapper_sig=>[[:rest, :rest], [:block, :block]],
:result=>
#<ArgumentError: wrong number of arguments (given 2, expected 1; required keyword: b)>}
What I think the sigature actually is
{:real_sig=>[[:req, :a], [:keyreq, :b]],
:wrapper_sig=>[[:rest, :rest], [:keyrest, :kwrest], [:block, :block]],
:result=>[123, {:b=>456}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment