Project

General

Profile

Actions

Feature #15419

open

Allow Kernel#tap to be invoked with arguments like Kernel#send

Added by shuber (Sean Huber) over 5 years ago. Updated over 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:90553]

Description

Tapping methods without any arguments already has nice shorthand via Symbol#to_proc:

object.tap { |o| o.example }
# vs
object.tap(&:example)

Unfortunately once other arguments are involved we have to switch back to the longer form:

array.merge(other).tap { |a| a.delete(object) }

This patch introduces a convenient and familiar shorthand for these cases which behaves similar to Kernel#send:

array.merge(other).tap(:delete, object)

Calling tap without any arguments or block still raises LocalJumpError:

3.tap #=> LocalJumpError: no block given

This also makes the existing shorthand even shorter:

object.tap { |o| o.example }
# vs
object.tap(&:example)
# vs
object.tap(:example)

Pull request: https://github.com/ruby/ruby/pull/2050

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0