Complete the code to merge two hashes using the merge method.
hash1 = {a: 1, b: 2}
hash2 = {b: 3, c: 4}
result = hash1.[1](hash2)
puts resultThe merge method combines two hashes and returns a new hash without modifying the original.
Complete the code to update a hash with another hash using the update method.
hash1 = {x: 10, y: 20}
hash2 = {y: 30, z: 40}
hash1.[1](hash2)
puts hash1The update method modifies the original hash by adding or replacing keys from another hash.
Fix the error in the code to correctly merge two hashes with a block to handle conflicts.
h1 = {a: 100, b: 200}
h2 = {b: 254, c: 300}
result = h1.merge(h2) { |key, old_val, new_val| [1] }
puts resultThe block tells merge how to combine values when keys overlap. Here, it adds the old and new values.
Fill both blanks to create a hash comprehension that merges two hashes and keeps the larger value for duplicate keys.
h1 = {a: 5, b: 10}
h2 = {b: 7, c: 12}
result = h1.merge(h2) { |[1], old_val, new_val| old_val [2] new_val ? old_val : new_val }
puts resultThe block compares old and new values and keeps the larger one. The first blank is the block variable for the key, usually key. The second blank is the comparison operator >.
Fill all three blanks to update a hash with another hash, but keep the original value if the new value is nil.
h1 = {a: 1, b: 2}
h2 = {b: nil, c: 3}
h1.update(h2) { |[1], old_val, new_val| new_val [2] nil ? new_val : [3] }
puts h1The block checks if the new value is not nil. If so, it uses the new value; otherwise, it keeps the old value. The first blank is the block variable for the key, usually key. The second blank is the not equal operator !=. The third blank is old_val to keep the original value.