Bird
0
0

Which of the following is the correct way to create a Ruby hash with a symbol key named :color and a string value "blue"?

easy📝 Conceptual Q1 of 15
Ruby - Hashes
Which of the following is the correct way to create a Ruby hash with a symbol key named :color and a string value "blue"?
A{ color: "blue" }
B{ :color => blue }
C{ "color" => :blue }
D{ color => "blue" }
Step-by-Step Solution
Solution:
  1. Step 1: Understand symbol key syntax

    In Ruby, symbol keys can be written as key: value or :key => value. The first option uses the newer syntax.
  2. Step 2: Check value type correctness

    The value should be a string "blue" enclosed in quotes. { color: "blue" } correctly uses color: "blue".
  3. Final Answer:

    { color: "blue" } -> Option A
  4. Quick Check:

    Symbol key hash = { color: "blue" } [OK]
Quick Trick: Use colon after symbol for key: { key: value } [OK]
Common Mistakes:
MISTAKES
  • Using unquoted strings as values
  • Confusing symbol and string keys
  • Missing colon after symbol key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes