On 2/2/2011 8:56 AM, Edmond Kachale wrote:
> Rubysters,
> 
> I think I am not wrong if I can solicit some knowledge. :-)
> 
> Can any one help modify this code?
> 
> I have:
> array = [['a', 1], ['b', 2], ['a',2], ['c',4], ['d', 5], ['b',4]]
> 
> I want to create a hash using inject method:
>>> hash = {'a' => [1, 2], 'b' => [2, 4], 'c' => [4], 'd' => [5]}

array.inject({}) do |result, (key, value)|
  (result[key] ||= []) << value
  result
end

-Jeremy