Bird
0
0

Given a hash scores = { alice: 90, bob: 55, carol: 75, dave: 40 }, how can you use reject to create a new hash excluding all entries with scores below 60?

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

    When rejecting from a hash, the block receives two parameters: key and value.
  2. Step 2: Apply condition on values

    We want to reject entries where the score (value) is less than 60, so the block should check score < 60.
  3. Step 3: Verify options

    scores.reject { |name, score| score < 60 } correctly uses both key and value parameters and checks the score. The other options either use only one block parameter or check the key instead of the value.
  4. Final Answer:

    scores.reject { |name, score| score < 60 } -> Option B
  5. Quick Check:

    Reject hash entries by value condition = B [OK]
Quick Trick: Use two block params for hash reject: |key, value| [OK]
Common Mistakes:
  • Using only one block parameter for hash reject
  • Checking key instead of value
  • Confusing reject with select for filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes