Ruby Way, 2nd Edition has a whole chapter on multi-valued hashes, if I remember correctly. Despite this, there are no built-in functions for this behavior.
However, you can have some fun with passing the block to Hash.new .
$ irb >> h = Hash.new { |hash, key| hash[key] = [] } => {} >> h[:a] << "Value1" => ["Value1"] >> h[:a] << "Value2" => ["Value1", "Value2"] >> h => {:a=>["Value1", "Value2"]} >>
If you want []= always add a value, you need to install the monkey patch. Again, nothing is created to work this way.
Travis
source share