[ruby-core:96238] [Ruby master Bug#16421] public_send with empty keyword arguments (Ruby version < 2.7)

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83130.20191214164421.44db4b4aad23adbf@ruby-lang.org>
Issue #16421 has been updated by zverok (Victor Shepelev).


@jeremyevans0 yeah, thanks, I see now.

----------------------------------------
Bug #16421: public_send with empty keyword arguments (Ruby version < 2.7)
https://bugs.ruby-lang.org/issues/16421#change-83130

* Author: zverok (Victor Shepelev)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Probably it is something that I've just missed, so it is more a question than bug report.
In Ruby < 2.7, I'd implement "delegate everything" method as such:
```ruby

def delegate(name, *args, &block)
  @something.public_send(name, *args, &block)
end
```
Now, while updating one of my gems to Ruby 2.7, I receive a warning (if SOME of the delegated methods accept keyword args):
```ruby
class A
  def simple_method(arg)
    p arg
  end

  def kwargs_method(arg, kwarg:)
    p [arg, kwarg]
  end

  def delegate(name, *args, &block)
    public_send(name, *args, &block)
  end
end

A.new.delegate(:kwargs_method, 1, kwarg: 2) # warning: The last argument is used as the keyword parameter
```
OK, that's understandable!
Now, I am trying to rewrite kode to work with both 2.6 and 2.7:
```ruby
class A
  def simple_method(arg)
    p arg
  end

  def kwargs_method(arg, kwarg:)
    p [arg, kwarg]
  end

  def delegate(name, *args, **kwargs, &block)
    public_send(name, *args, **kwargs, &block)
  end
end

A.new.delegate(:kwargs_method, 1, kwarg: 2)
A.new.delegate(:simple_method, 1)
```
It warks as expected under 2.7, but under 2.6, the last line throws:
```
tmp/delegate.rb:2:in `simple_method': wrong number of arguments (given 2, expected 1) (ArgumentError)
```

Is it something obvious that I am missing (I tried to search through the tracker, but can't detect related issues...). It there something I can do to have code working in both 2.6 and 2.7 without falling back to `ruby_2keywords` or `if RUBY_VERSION < '2.7'` (or checking for kwargs in `if method(name).parameters`)?..



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.