From: 7rans Date: 2009-08-31T01:08:35+09:00 Subject: Re: #map, #select semantics On Aug 29, 9:25 am, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: #map, #select semantics" >     on Sat, 29 Aug 2009 21:32:21 +0900, 7rans writes: > > |Let's say we have > | > |  [[:a,1], [:b,2]].map{ |(k,v)| [k, v+1] }  #=> [[:a,2], [:b,3]] > | > |We might have something like: > | > |[[:a,1], [:b,2]].using({}).map{|(k,v)| [k, v+1]}  #=> {:a=>2, :b=>3} > > [[:a,1], [:b,2]].inject({}){|h,(k,v)| h[k]=v+1; h}     #=> {:a=>2, :b=>3} Ok. There are lots of ways to do it. Often I actually will use the faster: h={};[[:a,1], [:b,2]].each{|(k,v)| h[k]=v+1}; h But the ideal was to also achieve: {:a=>1, :b=>2}.map{ |k,v| [k, v+1] } #=> {:a=>2, :b=>3} Which is far more clear then the alternatives (and easily converted to an Array with #to_a). The idea of #using is just icing on the cake, but is itself clearer than #inject (or the new #each_with_object), both of which strike me as primarily inventions to make up for the lack of the above, which I imagine represents a large chunk of the use cases. However, it does break backwards compatibility, so I don't expect it's uptake irregardless of it's merits.