Bird
0
0

Which syntax correctly sets the value of key :name to "Alice" in a Ruby hash person?

easy📝 Syntax Q12 of 15
Ruby - Hashes
Which syntax correctly sets the value of key :name to "Alice" in a Ruby hash person?
A<code>person[:name] == "Alice"</code>
B<code>person.name = "Alice"</code>
C<code>person["name"] = Alice</code>
D<code>person[:name] = "Alice"</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash key assignment syntax

    To set a value in a hash, use square brackets with the key and assign with =.
  2. Step 2: Analyze each option

    person[:name] = "Alice" correctly sets the key :name. person.name = "Alice" is invalid for hashes. person["name"] = Alice misses quotes around Alice, causing error. person[:name] == "Alice" is a comparison, not assignment.
  3. Final Answer:

    person[:name] = "Alice" -> Option D
  4. Quick Check:

    Use brackets and = for hash assignment [OK]
Quick Trick: Use brackets and = to set hash values [OK]
Common Mistakes:
MISTAKES
  • Using dot notation for hashes
  • Forgetting quotes around string values
  • Using == instead of = for assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes