Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Hashes
Identify the error in this Ruby code:
profile = { username: 'john_doe', email: 'john@example.com' }
puts profile[username]
Aemail key is missing
Busername should be a symbol or string key: profile[:username]
CHash keys must be strings only
Dputs cannot print hash values
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash key access

    Hash keys must be referenced as symbols or strings inside brackets.
  2. Step 2: Identify error

    profile[username] treats username as a variable, which is undefined.
  3. Step 3: Correct usage

    Use profile[:username] to access the value.
  4. Final Answer:

    username should be a symbol or string key: profile[:username] -> Option B
  5. Quick Check:

    Use symbols or strings as hash keys [OK]
Quick Trick: Use symbols or strings as keys in brackets [OK]
Common Mistakes:
  • Using bare words instead of symbols or strings as keys
  • Assuming keys are variables
  • Confusing hash syntax with method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes