Complete the code to create a hash with a default value of 0.
counts = Hash.new([1])Using Hash.new(0) sets the default value to 0 for any missing key.
Complete the code to create a hash with a default value of an empty array.
groups = Hash.new([1])Using Hash.new([]) sets the default value to the same empty array for all missing keys.
Fix the error in the code to correctly set a default value that is a new empty array for each missing key.
groups = Hash.new { |hash, key| hash[[1]] = [] }The block receives the hash and the missing key. Assigning hash[key] = [] creates a new empty array for each missing key.
Fill both blanks to create a hash that counts occurrences of words.
counts = Hash.new([1]) words.each do |word| counts[word] = counts[word] [2] 1 end
Setting default to 0 allows counting. Using '+' adds 1 to the current count.
Fill all three blanks to create a hash with default value as a new array and add elements to it.
groups = Hash.new { |hash, [1]| hash[[2]] = [3] }
groups[:fruits] << 'apple'The block takes the missing key as 'key'. Assigning hash[key] = [] creates a new empty array for each missing key, allowing elements to be added.