Bird
0
0

Given this hash:

hard📝 Application Q9 of 15
Ruby - Hashes
Given this hash:
inventory = { apples: 10, oranges: 5, bananas: 7 }
How can you create a new hash with only items that have quantity greater than 6?
Ainventory.filter { |k| k > 6 }
Binventory.select { |k, v| v > 6 }
Cinventory.map { |k, v| v > 6 }
Dinventory.delete_if { |k, v| v < 6 }
Step-by-Step Solution
Solution:
  1. Step 1: Use select to filter hash by value

    Select returns a new hash with pairs where the block is true.
  2. Step 2: Check condition v > 6

    Only keys with values greater than 6 are included.
  3. Final Answer:

    inventory.select { |k, v| v > 6 } -> Option B
  4. Quick Check:

    Use select with condition to filter hashes = A [OK]
Quick Trick: Use select { |k,v| condition } to filter hashes [OK]
Common Mistakes:
MISTAKES
  • Using filter with wrong block parameters
  • Using map which returns array, not hash
  • Using delete_if which modifies original hash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes