Bird
0
0

How do you correctly define a Ruby hash with symbol keys for :name and :age?

easy📝 Syntax Q3 of 15
Ruby - Hashes
How do you correctly define a Ruby hash with symbol keys for :name and :age?
A{ :name = 'Alice', :age = 30 }
B{ 'name' => 'Alice', 'age' => 30 }
C{ name: 'Alice', age: 30 }
D{ name => 'Alice', age => 30 }
Step-by-Step Solution
Solution:
  1. Step 1: Recognize symbol key syntax

    Ruby allows symbol keys using the syntax key: value without hash rockets.
  2. Step 2: Identify correct syntax

    { name: 'Alice', age: 30 } uses symbol keys correctly with colon after the key name.
  3. Final Answer:

    { name: 'Alice', age: 30 } -> Option C
  4. Quick Check:

    Symbol keys use colon after key name [OK]
Quick Trick: Symbol keys use colon after key name [OK]
Common Mistakes:
MISTAKES
  • Using hash rockets with symbol keys incorrectly
  • Using assignment (=) instead of colon (:)
  • Using strings instead of symbols for keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes