Bird
0
0

Which of the following is the shorthand way to define a Ruby hash with symbol keys for :name and :age?

easy📝 Syntax Q3 of 15
Ruby - Hashes
Which of the following is the shorthand way to 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 to be defined with the shorthand syntax: key: value, which is equivalent to :key => value.
  2. Step 2: Identify correct syntax

    { name: 'Alice', age: 30 } uses the shorthand symbol key syntax correctly.
  3. Final Answer:

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

    Symbol key shorthand syntax = B [OK]
Quick Trick: Use key: value for symbol keys, not quotes for strings [OK]
Common Mistakes:
  • Using strings instead of symbols for keys
  • Omitting colons for symbol keys
  • Using variables as keys without symbols

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes