From: "marcandre (Marc-Andre Lafortune)" Date: 2012-11-08T05:22:38+09:00 Subject: [ruby-core:49066] [ruby-trunk - Feature #7297] map_to alias for each_with_object Issue #7297 has been updated by marcandre (Marc-Andre Lafortune). Hi, bitsweat (Jeremy Kemper) wrote: > The common thread here is that people want a hash conversion in an enumerable chain, where it feels fluent and natural, rather than wrapping the result with Hash[] which makes the code read backward. #each_with_object is wonderful, but verbose for this use. > > Ruby has the idea of an association already: a key and value paired together. It's used by Array#assoc to look up a value from a list of pairs and by Hash#assoc to return a key/value pair. Building up a mapping of key/value pairs is *associating* keys with values. So consider Enumerable#associate which builds a mapping by *associating* keys with values: Exactly. Here's the secret plan I propose: 1) Let's convince Matz about Enumerable#to_h by adding your +1 to #7292. 2) When that's done, and only when that's done, we can convince Matz that 'associate' is the right name for a nifty method that associates keys with values. See my 1 min slide in #4151 . I believe my proposal agrees with yours and it also handles collisions and the special case of `Hash#associate`. Note that we should start with `Enumerable#to_h`. If we have it, then we can see it's probably best if `[].associate` (with no block) returns an enumerator, not a hash. 3) After that, we can see if we can come up with a general 'categorize' that could replace `map{ [k, v] }.to_h` as well as more complex nested cases. I'd also propose you move the discussion of these features from here (a rejected request about an alias of map_with_object!) to the right issues. ---------------------------------------- Feature #7297: map_to alias for each_with_object https://bugs.ruby-lang.org/issues/7297#change-32591 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/