Bird
0
0

Find the error in this Ruby code using a hash:

medium📝 Debug Q14 of 15
Ruby - Hashes
Find the error in this Ruby code using a hash:
settings = { theme: 'dark', font_size: 12 }
puts settings['theme']
AMissing comma between key-value pairs
BHash keys must be strings, not symbols
CUsing string key instead of symbol key causes nil output
DSyntax error due to missing curly braces
Step-by-Step Solution
Solution:
  1. Step 1: Check hash keys and access method

    The hash uses symbol keys (:theme), but the code tries to access with a string key ('theme').
  2. Step 2: Understand the result of wrong key type

    Accessing with a string key returns nil because the keys don't match.
  3. Final Answer:

    Using string key instead of symbol key causes nil output -> Option C
  4. Quick Check:

    Symbol keys need symbol access [OK]
Quick Trick: Match key types exactly when accessing hashes [OK]
Common Mistakes:
MISTAKES
  • Mixing string and symbol keys
  • Assuming keys auto-convert types
  • Ignoring nil return on missing keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes