current_stock with exact product names and stock countsnew_shipment with exact product names and stock countsmerge! method with a block to update current_stock by adding stock counts from new_shipmentcurrent_stock hashcurrent_stock with exact product names and stock countsnew_shipment with exact product names and stock countsmerge! method with a block to update current_stock by adding stock counts from new_shipmentcurrent_stock hashcurrent_stock with these exact entries: 'apple' => 30, 'banana' => 15, 'orange' => 20Use curly braces {} to create a hash. Separate keys and values with =>.
new_shipment with these exact entries: 'banana' => 10, 'orange' => 5, 'grape' => 12Use the same syntax as the first hash to create new_shipment.
merge! method on current_stock with new_shipment and a block that adds the old and new stock counts for matching productsThe block receives the key, old value, and new value. Return the sum to update the stock count.
current_stock hash to display the updated stock countsUse puts to print the hash. The output shows the updated stock counts.
Which Ruby method returns a new hash by combining two hashes without changing the original?
merge behaviormerge returns a new hash combining two hashes without modifying the original.merge! and updatemerge! and update modify the original hash, so they don't return a new one.merge returns new hash [OK]Which of the following is the correct syntax to update hash a with hash b in Ruby?
a = {x: 1}b = {y: 2}update is a method that modifies the original hash and takes another hash as argument with parentheses.merge! also updates but usually called with ! and parentheses are optional but update(b) is the most explicit and correct syntax here.update() to modify original hash [OK]What is the output of this Ruby code?
a = {a: 1, b: 2}b = {b: 3, c: 4}c = a.merge(b)
puts c
merge behavior with overlapping keysmerge returns a new hash combining keys; overlapping keys get values from the second hash.b (3) replaces value from a (2). Keys :a and :c are included as is.Identify the error in this Ruby code snippet:
a = {x: 10}b = {y: 20}a.merge! b
puts a
merge! method callmerge! can be called without parentheses in Ruby, so a.merge! b is valid syntax.merge! updates a with keys and values from b, so a becomes {x: 10, y: 20}. No errors occur.merge! updates original hash without error [OK]You have two hashes:
h1 = {a: 1, b: 2, c: 3}h2 = {b: 20, d: 4}You want to update h1 with h2 but only keep keys from h2 that have values greater than 10. Which code correctly does this?
h2 keys with values > 10select { |k, v| v > 10 } to keep only keys with values greater than 10.h1 with filtered h2update to modify h1 in place with the filtered hash.merge! also updates but update is clearer; merge returns new hash and does not modify h1.Pass the test and this pattern moves unknown