Ruby Hash - Initialization

  • + 0 comments

    a few points to keep in mind: Use {} for a normal hash. Hash.new(0) works well for counters. Avoid Hash.new([]) since it shares the same array, instead use Hash.new { |h, k| h[k] = [] } to create separate arrays. For nested counters, you can try Hash.new { |h, k| h[k] = Hash.new(0) }. You can also build a hash from key-value pairs with Hash[...].