Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shuber
Copy link

@shuber shuber commented Dec 15, 2018

Ruby issue: https://bugs.ruby-lang.org/issues/15419


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 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)

@shuber shuber changed the title Allow Kernel#tap to be invoked with arguments like Kernel#send Allow Kernel#tap to be invoked with arguments like Kernel#send Dec 15, 2018
@shuber shuber force-pushed the sh/enhance-tap branch 7 times, most recently from 0529321 to 4162599 Compare December 16, 2018 19:49
@k0kubun k0kubun changed the base branch from trunk to master August 15, 2019 17:25
@k0kubun
Copy link
Member

k0kubun commented Aug 17, 2019

It seems to have a conflict now. Could you rebase this from master?

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

```ruby
object.tap { |o| o.example }
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 a convenient 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 }
object.tap(&:example)
object.tap(:example)
```

---

Ruby issue: https://bugs.ruby-lang.org/issues/15419
@shuber
Copy link
Author

shuber commented Aug 18, 2019

@k0kubun rebased master and resolved the conflict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants