Bird
0
0

Which of the following is a syntax error when creating a Ruby hash?

easy📝 Syntax Q3 of 15
Ruby - Hashes
Which of the following is a syntax error when creating a Ruby hash?
A{ :age => 30, name: "Bob" }
B{ age: 30 name: "Bob" }
C{ "age" => 30, :name => "Bob" }
D{ age: 30, "name" => "Bob" }
Step-by-Step Solution
Solution:
  1. Step 1: Check commas between pairs

    Ruby hashes require commas between key-value pairs. { age: 30 name: "Bob" } misses a comma between 30 and name:.
  2. Step 2: Validate other options

    Options A, B, and C correctly separate pairs with commas and use valid syntax.
  3. Final Answer:

    { age: 30 name: "Bob" } -> Option B
  4. Quick Check:

    Missing commas cause syntax errors [OK]
Quick Trick: Always separate hash pairs with commas [OK]
Common Mistakes:
  • Omitting commas between pairs
  • Mixing symbol and string keys incorrectly
  • Using invalid key syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes