Bird
0
0

What will this code output?

hard📝 Application Q9 of 15
Ruby - Blocks, Procs, and Lambdas
What will this code output?
hash = {a: 1, b: 2, c: 3}
new_hash = hash.map { |k, v| [k, v * 2] }.to_h
puts new_hash.inspect
A[[:a, 2], [:b, 4], [:c, 6]]
B{:a=>2, :b=>4, :c=>6}
C{a: 2, b: 4, c: 6}
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand map on a hash

    Mapping over a hash yields key-value pairs. The block returns an array with key and doubled value.
  2. Step 2: Convert array of pairs back to hash

    The to_h method converts the array of key-value pairs back into a hash with doubled values.
  3. Final Answer:

    {:a=>2, :b=>4, :c=>6} -> Option B
  4. Quick Check:

    map and to_h transform hash values = C [OK]
Quick Trick: Use map with to_h to transform hashes easily [OK]
Common Mistakes:
  • Expecting symbol keys without rocket syntax
  • Confusing array output with hash
  • Forgetting to convert back to hash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes