Ruby create hash of arrays

November 22, 2020

How to create a hash of arrays easily.

Hash.new {|h, k| h[k] = [] }

Example of how to use in rails:

Example.all.reduce(Hash.new {|h, k| h[k] = [] }) do |results, ex|
  results[ex.id] = ex
  results
end

Search