Issue #7297 has been updated by trans (Thomas Sawyer).


=begin
But the crux of the problem is simply that #each_with_object has a name that it is too long, which greatly deters usage. I know I am loathe to use it even when it would be useful for this simple reason. And that has also has a lot to do with the fact that the last word of the method's name, "_object", is complete redundant. Of course it is a object! Ruby is an OOPL! So just shortening it to #each_with alone would make a big difference.

Beyond that, one might argue the block parameters should be in the opposite order to align with #inject, but that isn't at all important. More significant would be to default the argument to an empty hash, since that would be the most common case. I think that's a good idea so the common case can be more concise. But in doing that, it makes more sense to call the method #map_with and have it return the object.

So the doubles example would be:

    doubles = numbers.map_with{ |n, h| h[n] = n * 2 }

And one could also do things like:

    doubles = numbers.map_with([]){ |n, a| a << n * 2 }

It's a very versatile method and the name makes sense.
=end
----------------------------------------
Feature #7297: map_to alias for each_with_object
https://bugs.ruby-lang.org/issues/7297#change-32567

Author: nathan.f77 (Nathan Broadbent)
Status: Rejected
Priority: Normal
Assignee: 
Category: lib
Target version: 2.0.0


I would love to have a shorter alias for 'each_with_object', and would like to propose 'map_to'. Here are my arguments:

* It reads logically and clearly:

[1, 2, 3].map_to({}) {|i, hash| hash[i] = i ** 2 }
#=> {1 => 1, 2 => 4, 3 => 9}

* Rubyists are already using 'map' to build and return an array, so it should be obvious that 'map_to(object)' can be used to build and return an object.

* Given that 'each' and 'each_with_index' return the original array, I feel that the 'each_with_object' method name is slightly counterintuitive. 'map_to' might not be 100% semantically correct, but it's obvious that it will return something other than the original array.

* Many people (myself included) were using inject({}) {|hash, el| ... ; hash } instead of 'each_with_object', partly because of ignorance, but also because 'each_with_object' is so long. 'map_to' is the same length as inject, and means that you don't have to return the object at the end of the block.

* Only a single line of code is needed to implement the alias.


Best,
Nathan


-- 
http://bugs.ruby-lang.org/