From: merch-redmine@... Date: 2019-12-14T16:36:44+00:00 Subject: [ruby-core:96237] [Ruby master Bug#16421] public_send with empty keyword arguments (Ruby version < 2.7) Issue #16421 has been updated by jeremyevans0 (Jeremy Evans). Status changed from Open to Closed You should use `ruby2_keywords`: ```ruby def delegate(name, *args, &block) public_send(name, *args, &block) end ruby2_keywords :delegate if respond_to?(:ruby2_keywords, true) ``` Then things will work correctly. This type of code is the reason that `ruby2_keywords` was introduced. ---------------------------------------- Bug #16421: public_send with empty keyword arguments (Ruby version < 2.7) https://bugs.ruby-lang.org/issues/16421#change-83129 * 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: