[ruby-core:117627] [Ruby master Bug#20440] `super` from child class passing keyword arg as Hash if in a method with passthrough args called from base class
From:
"Eregon (Benoit Daloze) via ruby-core" <ruby-core@...>
Date:
2024-04-20 11:04:33 UTC
List:
ruby-core #117627
Issue #20440 has been updated by Eregon (Benoit Daloze).
See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
it also explains `(*, **)` and `(...)` which are better if you don't need compatibility with Ruby < 2.7.
----------------------------------------
Bug #20440: `super` from child class passing keyword arg as Hash if in a method with passthrough args called from base class
https://bugs.ruby-lang.org/issues/20440#change-108034
* Author: ozydingo (Andrew Schwartz)
* Status: Open
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Apologies for the verbose title, but that's the specific set of conditions that AFAICT are required to reproduce the bug!
Here's the simplest setup I can reproduce:
```rb
class Base
def foo(*args, x: 1)
puts "Base: calling foo with args: #{args}, x: #{x}"
end
def foo!(x: 1)
puts "Base: calling foo! with x: #{x}"
foo(x: x)
end
end
class Child < Base
def foo(*)
puts "Child: calling foo"
super
end
end
```
When I call `Child.new.foo!`, I expect it to call the base class method `foo!`, which will use the default keyword arg `x: 1`; then the child method `foo` with `x: 1`, and finally the base method `foo` with `x: 1`. However, this is not what I observe:
```rb
Child.new.foo!
Base: calling foo! with x: 1
Child: calling foo
Base: calling foo with args: [{:x=>1}], x: 1
```
So when the child `foo` method called `super`, it passed not only `x: 1` as a keyword arg, but *also* `{x: 1}` as a Hash positional arg to the super method.
This is breaking my upgrade to Ruby 3.0 as I have a similar setup but without the `*args` param, this I am getting the error "wrong number of arguments (given 1, expected 0)".
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- [email protected]
To unsubscribe send an email to [email protected]
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/