From: sean.m.huber@... Date: 2018-12-16T20:02:24+00:00 Subject: [ruby-core:90574] [CommonRuby Feature#15419] Allow Kernel#tap to be invoked with arguments like Kernel#send Issue #15419 has been updated by shuber (Sean Huber). > Eregon (Benoit Daloze) wrote: > Yes, in such a case you need to use `ruby_version_is` guards as documented on https://github.com/ruby/spec/blob/master/CONTRIBUTING.md#guards, > because the behavior of previous versions will not change (unless this is decided to be backported, but very unlikely for a new feature, and even then the backport would be done later so the guards would be needed at first). Perfect thanks @Eregon, [pull request](https://github.com/ruby/ruby/pull/2050) updated! > oleynikov (Alexander Oleynikov) wrote: > And I'm not sure why we need to change just #tap and not other methods. > There are some discussions going on about passing arguments whith Symbol#to_proc shorthand: #12115, #15301, etc. Personally I'd rather see one of these implemented. It would also fix this issue. Thanks for the links to the other discussions @oleynikov, pretty interesting, I'll keep an eye on those as well! For `Kernel#tap` specifically I think it still makes sense to allow it to accept arguments like `Kernel#send` since they basically behave the same aside from `tap` always returning `self`. Relying on the proposals to support `Symbol#to_proc` with arguments (which could take awhile until they're actually merged) looks a bit more abstract than it needs to be: ```ruby array.tap(:delete, object) # vs array.tap(&:delete.(object)) # if that's the syntax they agree on # vs array.tap { |a| a.delete(object) } # currently supported ``` ---------------------------------------- Feature #15419: Allow Kernel#tap to be invoked with arguments like Kernel#send https://bugs.ruby-lang.org/issues/15419#change-75727 * Author: shuber (Sean Huber) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Tapping methods without any arguments already has nice shorthand via `Symbol#to_proc`: ```ruby object.tap { |o| o.example } # vs object.tap(&:example) ``` Unfortunately once other arguments are involved we have to switch back to the longer form: ```ruby array.merge(other).tap { |a| a.delete(object) } ``` [This patch introduces](https://github.com/ruby/ruby/pull/2050) a convenient and familiar shorthand for these cases which behaves similar to `Kernel#send`: ```ruby array.merge(other).tap(:delete, object) ``` Calling tap without any arguments or block still raises `LocalJumpError`: ```ruby 3.tap #=> LocalJumpError: no block given ``` This also makes the existing shorthand even shorter: ```ruby object.tap { |o| o.example } # vs object.tap(&:example) # vs object.tap(:example) ``` --- **Pull request**: https://github.com/ruby/ruby/pull/2050 -- https://bugs.ruby-lang.org/ Unsubscribe: