From: "Jesús Gabriel y Galán" Date: 2011-02-03T01:10:45+09:00 Subject: Re: Soliciting some help with inject method > But I also value you contribution, Adam and Jesûs. In fact, I think your > methods are faster than the inject's way. You'd be surprised :-). If I didn't do anything wrong (I usually make mistakes when benchmarking), my method is the slowest: require 'benchmark' TIMES = 100_000 array = [['a', 1], ['b', 2], ['a',2], ['c',4], ['d', 5], ['b',4]] Benchmark.bm do |x| x.report "inject Jeremy" do TIMES.times do array.inject({}) do |result, (key, value)| (result[key] ||= []) << value result end end end x.report "Adam" do TIMES.times do result = {} array.each { |k, v| result[k] ||= []; result[k] << v } end end x.report "inject Andrew" do TIMES.times do hash = array.inject({}) do |result, values| key,val = values if result[key].nil? result[key] = [val] else result[key].push val end result end end end x.report "Jesus" do TIMES.times do result = Hash.new {|h,k| h[k] = []} array.each {|key,value| result[key] << value} end end end $ ruby bm_inject.rb user system total real inject Jeremy 1.440000 0.300000 1.740000 ( 1.770496) Adam 1.100000 0.190000 1.290000 ( 1.290073) inject Andrew 1.810000 0.210000 2.020000 ( 2.064162) Jesus 1.800000 0.320000 2.120000 ( 2.118433) I guess it's because you need to call the default proc many times, as opposed to the other methods, which don't have any proc to call or block to yield to. Jesus.