Explain the difference between Hash#merge and Hash#merge!

Beginner

Answer

  • merge: Returns a new hash with combined key-value pairs
  • merge!: Modifies the original hash in place (destructive)
h1 = { a: 1, b: 2 }
h2 = { b: 3, c: 4 }
h1.merge(h2)   # Returns { a: 1, b: 3, c: 4 }, h1 unchanged
h1.merge!(h2)  # h1 is now { a: 1, b: 3, c: 4 }