Feature #15145


chained mappings proposal

Added by koenhandekyn (koen handekyn) about 5 years ago. Updated about 5 years ago.

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, &: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)

proposal to be functional equivalent to { |e| e&.instrument }.map { |e| e&.issuer }.map { |e| e&.name }
Actions #1

Updated by koenhandekyn (koen handekyn) about 5 years ago

  • Description updated (diff)

Updated by shevegen (Robert A. Heiler) about 5 years ago

(I think you filed this in the wrong section; right now it is under Bugs, but it looks
like a feature so it should go into )

At any rate, to the suggestion itself:

I am already biased because I am not a big fan of object&.method for various reasons,
some of which are a personal preference (but also because my eyesight is not the best).

However had, you don't have to convince me - you ultimately only have to convince
matz (and it helps to convince the core team too).

Aside from any personal bias, I think there may be more objective reasons against the
proposal from a syntax and "meaning" point of view.

I may be mistaken, but I believe

always meant a to_proc call (e. g. ).

This was probably also one reason why it could not too easily be extended to
also allow for arguments passed into it, e. g. anything where you
pass stuff into the method that is called there.

Having suddenly mean { |e| e&.foo } is, I think,
re-defining existing behaviour. Perhaps there are cases where this new behaviour
would be undesired? But I may be wrong; I make use of & only very sparingly
so in the ruby code I write/use.

Aside from this, the net gain seems to be fairly small.

I understand succinctness being a good thing usually but I don't really see any
real net gain here.

As for intermediate nil values - to be honest I think it is possible to design
methods in such a way as to act as a filter, and discard nil values when they
are not necessary to begin with (e. g. making more use of .compact). But as
written above, you ultimately have to only convince matz. You can suggest to
add it to the next developer discussion to get the opinion(s) of the core
devs if you would like to, at:

Updated by jeremyevans0 (Jeremy Evans) about 5 years ago

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

koenhandekyn (koen handekyn) wrote:

proposal allow, &: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:

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: { |e| e&.instrument&.issuer&.name }

Also available in: Atom PDF