Bird
0
0

What happens if you use each on an empty array in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Enumerable and Collection Processing
What happens if you use each on an empty array in Ruby?
AIt returns nil immediately.
BThe block is never executed and no output is produced.
CIt executes the block once with nil.
DIt raises an error because there are no elements.
Step-by-Step Solution
Solution:
  1. Step 1: Understand each behavior on empty collections

    If the array is empty, there are no elements to iterate over, so the block does not run.
  2. Step 2: Confirm no error or output occurs

    No error is raised, and no output is produced because the block never executes.
  3. Final Answer:

    The block is never executed and no output is produced. -> Option B
  4. Quick Check:

    Empty array each = no block execution [OK]
Quick Trick: Empty collections skip the block silently [OK]
Common Mistakes:
  • Expecting error on empty array
  • Thinking block runs once with nil
  • Confusing return value with output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes