Bird
0
0

How can you use dig to safely access the first element of an array nested inside a hash?

hard📝 Application Q9 of 15
Ruby - Hashes
How can you use dig to safely access the first element of an array nested inside a hash?
Example: h = {arr: [10, 20, 30]}
Ah.dig(:arr)[0]
Bh.dig(:arr, 0)
Ch[:arr].dig(0)
Dh.dig(:arr, :0)
Step-by-Step Solution
Solution:
  1. Step 1: Understand dig with arrays

    dig can take integer indexes to access array elements nested inside hashes.
  2. Step 2: Use dig with symbol key and index

    Calling h.dig(:arr, 0) accesses the first element (10) safely.
  3. Final Answer:

    h.dig(:arr, 0) -> Option B
  4. Quick Check:

    dig accepts integer indexes for arrays [OK]
Quick Trick: Use dig with symbol key and integer index for arrays [OK]
Common Mistakes:
  • Using string index instead of integer
  • Calling dig on array directly
  • Using symbol for array index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes