Bird
0
0

What does the each method do when called on a Ruby hash?

easy📝 Conceptual Q2 of 15
Ruby - Hashes
What does the each method do when called on a Ruby hash?
AReturns an array of keys
BIterates over each key-value pair
CReturns an array of values
DDeletes all key-value pairs
Step-by-Step Solution
Solution:
  1. Step 1: Recall the behavior of each on a hash

    The each method runs a block for every key-value pair in the hash.
  2. Step 2: Identify the correct description

    It does not return arrays but yields pairs, so it iterates over each key-value pair.
  3. Final Answer:

    Iterates over each key-value pair -> Option B
  4. Quick Check:

    each method = iteration over pairs [OK]
Quick Trick: each runs code for every key-value pair [OK]
Common Mistakes:
  • Thinking each returns keys or values
  • Confusing each with keys or values methods
  • Assuming each deletes pairs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes