Bird
0
0

Find the bug in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Hashes
Find the bug in this Ruby code snippet:
options = { color: 'blue', size: 'M' }
puts options[:colour]
ATypo in key name :colour instead of :color
BHash keys must be strings, not symbols
CMissing comma between key-value pairs
DCannot use puts with hashes
Step-by-Step Solution
Solution:
  1. Step 1: Compare key names used

    The hash has key :color but code accesses :colour, which is a typo.
  2. Step 2: Understand result of wrong key access

    Accessing a non-existent key returns nil, so puts prints nothing.
  3. Final Answer:

    Typo in key name :colour instead of :color -> Option A
  4. Quick Check:

    Key names must match exactly = C [OK]
Quick Trick: Check spelling of keys carefully when accessing hashes [OK]
Common Mistakes:
MISTAKES
  • Assuming similar keys are interchangeable
  • Using string keys when symbols are defined
  • Ignoring nil results from wrong keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes