Issue #15145 has been updated by jeremyevans0 (Jeremy Evans).

Tracker changed from Bug to Feature
Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)

koenhandekyn (koen handekyn) wrote:
> proposal allow
> 
> ~~~ ruby
> collection.map(&:instrument, &:issuer, &:name)
> ~~~
> 
> implementation is trivial 

Is it possible for you to share your trivial implementation of passing more than one block to a method? :)

It is true that the implementation of something like:

~~~ ruby
collection.chained_map(:instrument.to_proc, :issuer.to_proc, :name.to_proc)
~~~

is fairly trivial, but it could easily be added via an external gem.  I don't think it makes sense to add a `Enumerable#chained_map` core method or to modify the `Enumerable#map` core method to accept arguments.  And I think adding the ability for a method to accept multiple blocks would be another feature request entirely.

In my opinion, it would probably be clearer to just use `map` with a single block:

~~~ ruby
collection.map { |e| e&.instrument&.issuer&.name }
~~~

----------------------------------------
Feature #15145: chained mappings proposal
https://bugs.ruby-lang.org/issues/15145#change-74144

* Author: koenhandekyn (koen handekyn)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
propsal, have map accept array of method references to make chained mapping simpler  with suggestion to implement it to behave like the &. operator so that intermediate nil values just ripple through as nil values

proposal allow

~~~ ruby
collection.map(&:instrument, &:issuer, &:name)
~~~

implementation is trivial 

this as a conceptual alternative to (with important remark is that if first or second mapping returns nil values the above code will break, forcing a much more verbose notation)

~~~ ruby
collection.map(&:instrument).map(&:issuer).map(&:name)
~~~

proposal to be functional equivalent to

~~~ ruby
collection.map { |e| e&.instrument }.map { |e| e&.issuer }.map { |e| e&.name }
~~~






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