Bird
0
0

Why does the each method on a Ruby hash yield two values to the block?

hard📝 Conceptual Q10 of 15
Ruby - Hashes
Why does the each method on a Ruby hash yield two values to the block?
ABecause Ruby hashes are arrays of arrays internally
BBecause each always yields two values regardless of collection
CBecause keys and values are stored separately and must be combined
DBecause a hash stores key-value pairs, so each iteration yields both
Step-by-Step Solution
Solution:
  1. Step 1: Recall hash structure

    A Ruby hash stores data as key-value pairs.
  2. Step 2: Understand each method behavior

    When iterating, each yields both key and value to the block for each pair.
  3. Final Answer:

    Because a hash stores key-value pairs, so each iteration yields both -> Option D
  4. Quick Check:

    each yields key and value pairs from hash [OK]
Quick Trick: each yields key and value because hash stores pairs [OK]
Common Mistakes:
  • Thinking each yields two values always
  • Confusing internal storage with iteration
  • Assuming keys and values are separate arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes