Bird
0
0

Given a hash scores = {alice: 10, bob: 20, carol: 30}, how do you update Bob's score to 50 and add a new key :dave with value 40 in one line?

hard📝 Application Q8 of 15
Ruby - Hashes
Given a hash scores = {alice: 10, bob: 20, carol: 30}, how do you update Bob's score to 50 and add a new key :dave with value 40 in one line?
Ascores[bob], scores[dave] = 50, 40
Bscores.update(bob: 50, dave: 40)
Cscores[:bob] = 50 scores[:dave] = 40
Dscores[:bob] == 50 && scores[:dave] == 40
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash update method

    The update method merges new key-value pairs into the hash.
  2. Step 2: Use update to change and add keys in one call

    Calling scores.update(bob: 50, dave: 40) updates Bob's score and adds Dave.
  3. Final Answer:

    scores.update(bob: 50, dave: 40) -> Option B
  4. Quick Check:

    Use update to set multiple keys at once [OK]
Quick Trick: Use update method to set multiple hash keys at once [OK]
Common Mistakes:
  • Trying multiple assignments separated by commas
  • Using == instead of = for assignment
  • Using semicolon for one-line but not updating hash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes