From: "rosenfeld (Rodrigo Rosenfeld Rosas)" Date: 2012-11-07T21:52:31+09:00 Subject: [ruby-core:49030] [ruby-trunk - Feature #7297] map_to alias for each_with_object Issue #7297 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas). I just searched each_with_object in GitHub and aside from some specs testing each_with_object all real-use cases I could see after navigating though several pages of the results were ".each_with_object({})...": https://github.com/search?langOverride=&language=Ruby&q=each_with_object&repo=&start_value=15&type=Code That is why I think we should have a .hash_map method instead of another generic one: double = numbers.hash_map{|h, n| h[n] = n * 2 } Better to read than double = numbers.each_with_object({}){|n, h| h[n] = n * 2 } But other than that, I don't really think map_to is a bad name for the generic case (I'm replacing the arguments order of the block since it makes more sense to me instead of using it as an alias to each_with_object): double = numbers.map_to({}){|h, n| h[n] = n * 2 } I read this as "map numbers to hash" which makes lots of sense to me. Much more than each_with_object. Which only seems to be used with hashes by the way... ---------------------------------------- Feature #7297: map_to alias for each_with_object https://bugs.ruby-lang.org/issues/7297#change-32556 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/