Bird
0
0

Given a hash scores = { alice: 90, bob: 75, carol: 85, dave: 60 }, how can you use reject to create a new hash excluding entries with scores less than 80?

hard📝 Application Q8 of 15
Ruby - Enumerable and Collection Processing
Given a hash scores = { alice: 90, bob: 75, carol: 85, dave: 60 }, how can you use reject to create a new hash excluding entries with scores less than 80?
Ascores.reject { |name, score| score < 80 }
Bscores.reject { |name| name < 80 }
Cscores.reject { |score| score < 80 }
Dscores.reject(&:score < 80)
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash reject block parameters

    Reject block receives two parameters: key and value (name and score).
  2. Step 2: Use correct condition on score

    We want to reject entries where score is less than 80, so condition is score < 80.
  3. Final Answer:

    scores.reject { |name, score| score < 80 } -> Option A
  4. Quick Check:

    reject hash entries by value condition [OK]
Quick Trick: Hash reject block takes |key, value| parameters [OK]
Common Mistakes:
  • Using only one block parameter for hash
  • Trying to use symbol-to-proc with condition
  • Applying condition to key instead of value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes