From: Rodrigo Rosenfeld Rosas Date: 2012-10-31T06:01:19+09:00 Subject: [ruby-core:48631] Re: [ruby-trunk - Feature #7241] Enumerable#to_h proposal Em 30-10-2012 16:23, Aaron Patterson escreveu: > On Tue, Oct 30, 2012 at 07:58:33PM +0900, rosenfeld (Rodrigo Rosenfeld Rosas) wrote: >> Issue #7241 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas). >> >> >> Maybe .hash_map? each_with_object is a too long name for a very common needed method. Many have asked for a method like it (including me) because they couldn't find "each_with_object" and they ended up learning here after asking for such a method. >> >> Maybe "hash_map" could be a better name for this. > `each_with_object` isn't specific to hashes, and isn't doing list > translation like `map` does. > > IOW, it sounds perfect for ActiveSupport. ;-) > I often have this requirement and I guess others have it as well. There are two problems with each_with_object in my opinion: 1 - you can't find it easily in the docs when you're looking for some way to "inject" a Hash without worrying about the result of the block; hash_map would be easier to find in the docs for newcomers (to each_with_object I mean, like I was less then an year ago if I remember correctly); 2 - it is a too long name. See examples below: hash = a_long_array_name_as_I_usually_use_for_my_variables.each_with_object({}){|(name, url), h| h[name] = url } h = {}; a_long_array_name_as_I_usually_use_for_my_variables.each{|(name, url)| h[name] = url }; hash = h Often in my methods I don't really need that extra (; hash = h) so it is usually much shorter when I don't use each_with_object. With proposed method: hash = a_long_array_name_as_I_usually_use_for_my_variables.hash_map{|h, (name, url)| h[name] = url } Notice that I changed the order of the arguments for the block. It makes more sense to me this way, just like inject. I know this is subjective but I find the last example better to read ;) Cheers, Rodrigo.