Bird
0
0

Given a hash scores = { alice: 85, bob: 42, carol: 75, dave: 30 }, how do you select only the key-value pairs where the value is greater than 50?

hard📝 Application Q8 of 15
Ruby - Enumerable and Collection Processing
Given a hash scores = { alice: 85, bob: 42, carol: 75, dave: 30 }, how do you select only the key-value pairs where the value is greater than 50?
Ascores.select { |k, v| v > 50 }
Bscores.filter { |k| k > 50 }
Cscores.select { |v| v > 50 }
Dscores.map { |k, v| v > 50 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering a hash with select

    When filtering a hash, select takes two block variables: key and value.
  2. Step 2: Use correct block variables and condition

    scores.select { |k, v| v > 50 } correctly uses { |k, v| v > 50 } to filter pairs with values greater than 50.
  3. Final Answer:

    scores.select { |k, v| v > 50 } -> Option A
  4. Quick Check:

    Use two block variables for hash select [OK]
Quick Trick: Use |key, value| to filter hashes with select [OK]
Common Mistakes:
  • Using only one block variable for hash
  • Using map instead of select
  • Filtering keys instead of values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes