Bird
0
0

Find the problem in this code:

medium📝 Debug Q7 of 15
Ruby - Hashes
Find the problem in this code:
h = {a: {b: 2}}
puts h.dig(:a, :b, :c)
ANo problem, outputs nil
BError because :b is not a hash
CSyntax error in dig usage
DOutputs 2
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of key :b

    Key :b has value 2, which is an integer, not a hash.
  2. Step 2: Understand dig behavior when next key is called on non-hash

    Calling dig(:c) on integer 2 causes a NoMethodError.
  3. Final Answer:

    Error because :b is not a hash -> Option B
  4. Quick Check:

    dig fails if intermediate value is not hash or array [OK]
Quick Trick: dig fails if intermediate value is not a container [OK]
Common Mistakes:
MISTAKES
  • Assuming nil returned
  • Ignoring type of intermediate value
  • Thinking dig always safe

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes