[ruby-core:78326] [Ruby trunk Feature#12760] Optional block argument for `itself`
From:
matz@...
Date:
2016-11-25 08:10:39 UTC
List:
ruby-core #78326
Issue #12760 has been updated by Yukihiro Matsumoto.
`yield_self` is OK, but I don't think we are going to add `object.{|x| ... }`.
Matz.
----------------------------------------
Feature #12760: Optional block argument for `itself`
https://bugs.ruby-lang.org/issues/12760#change-61691
* Author: Victor Shepelev
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
That's an another attempt to choose good metaphor for `object.(yield self)` (previously: #6721 and ton of duplicates).
In discussion here: https://bugs.ruby-lang.org/issues/11717#note-3 it was said:
> Clearly this is something the Ruby community wants. Just as clearly, it's something nobody can name.
But suddenly... I've thought about this!
```ruby
some.long.method.chain.constructing.string
.itself { |s| "(" + s + ")" }
.itself(&method(:puts))
# or
require 'open-uri'
construct_url(*params)
.itself(&method(:open))
.read
.itself(&JSON.method(:parse))
.to_yaml
.itself(&File.method(:write))
# NB: I understand that _last_ `itself` in both examples
# could as well be `tap`. But not all the previous.
```
Pros:
* method is already here, it has pretty name and underused (almost nothing except `group_by(&:itself)` comes to mind);
* it is not 100% good English, but readable: `itself(&JSON.method(:parse))` = "parse itself with json";
* implementation is trivial, no backwards-compatibility issues (like new methods shadowing something important in third-party library) are expected.
Cons:
* ???
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>