Bird
0
0

Which of the following is the correct syntax to set the value of key :city to "Paris" in a Ruby hash named location?

easy📝 Syntax Q3 of 15
Ruby - Hashes
Which of the following is the correct syntax to set the value of key :city to "Paris" in a Ruby hash named location?
Alocation.set(:city, "Paris")
Blocation.city = "Paris"
Clocation[:city] == "Paris"
Dlocation[:city] = "Paris"
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash assignment syntax

    To set a value in a hash, use square brackets with the key and assign with =.
  2. Step 2: Apply correct syntax for symbol key

    Use location[:city] = "Paris" to set the value.
  3. Final Answer:

    location[:city] = "Paris" -> Option D
  4. Quick Check:

    Set hash value = location[:city] = "Paris" [OK]
Quick Trick: Use square brackets and = to set hash values [OK]
Common Mistakes:
  • Using dot notation for hashes
  • Using == instead of = for assignment
  • Using non-existent set method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes