Bird
0
0

You have a nested data structure:

hard📝 Application Q15 of 15
Ruby - Hashes
You have a nested data structure:
settings = {theme: {colors: [{name: "red", code: "#f00"}, {name: "green", code: "#0f0"}]}}

Which dig call correctly retrieves the color code for the second color?
Asettings.dig(:theme, :colors)[1][code]
Bsettings.dig(:theme, :colors, 1, :code)
Csettings.dig(:theme, :colors, :code, 1)
Dsettings.dig(:theme, :colors, 2, :code)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the nested structure

    colors is an array of hashes. The second color is at index 1 (arrays start at 0).
  2. Step 2: Use dig with correct keys and index

    To get the code of the second color, use dig(:theme, :colors, 1, :code). settings.dig(:theme, :colors, 1, :code) matches this.
  3. Final Answer:

    settings.dig(:theme, :colors, 1, :code) -> Option B
  4. Quick Check:

    dig accesses nested array index and hash key [OK]
Quick Trick: Use array index as integer in dig for arrays [OK]
Common Mistakes:
  • Using wrong index (2 instead of 1)
  • Passing keys in wrong order
  • Trying to dig with symbol on array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes