Bird
0
0

Given the code:

medium📝 Predict Output Q13 of 15
Ruby - Hashes
Given the code:
data = {user: {profile: {name: "Alice", age: 30}}}
puts data.dig(:user, :profile, :name)

What will be the output?
A30
Bnil
CAlice
DError
Step-by-Step Solution
Solution:
  1. Step 1: Trace the dig method call

    The dig method looks for the key :user in data, then :profile inside that, then :name inside profile.
  2. Step 2: Extract the value at the nested keys

    All keys exist, so dig returns the string "Alice".
  3. Final Answer:

    Alice -> Option C
  4. Quick Check:

    dig returns nested value "Alice" [OK]
Quick Trick: Follow keys step-by-step to find nested value [OK]
Common Mistakes:
  • Expecting nil if keys exist
  • Confusing age with name value
  • Thinking dig raises error on existing keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes