0
0
Rubyprogramming~15 mins

Reject for inverse filtering in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Reject for inverse filtering
📖 Scenario: You work in a store that sells fruits. You have a list of fruits with their prices. You want to find all fruits that are not expensive, meaning their price is less than 5.
🎯 Goal: Build a Ruby program that uses reject to filter out expensive fruits from the list, keeping only the affordable ones.
📋 What You'll Learn
Create a hash called fruits with these exact entries: 'apple' => 3, 'banana' => 2, 'cherry' => 7, 'date' => 6, 'elderberry' => 1
Create a variable called price_limit and set it to 5
Use reject on fruits with a block that removes fruits with price greater than or equal to price_limit
Print the resulting hash of affordable fruits
💡 Why This Matters
🌍 Real World
Filtering items based on conditions is common in shopping apps, inventory management, and data analysis.
💼 Career
Knowing how to filter collections efficiently is important for software developers working with data and user interfaces.
Progress0 / 4 steps
1
Create the fruits hash
Create a hash called fruits with these exact entries: 'apple' => 3, 'banana' => 2, 'cherry' => 7, 'date' => 6, 'elderberry' => 1
Ruby
Need a hint?

Use curly braces {} to create a hash with keys and values separated by =>.

2
Set the price limit
Create a variable called price_limit and set it to 5
Ruby
Need a hint?

Just write price_limit = 5 to create the variable.

3
Filter out expensive fruits using reject
Use reject on fruits with a block that removes fruits with price greater than or equal to price_limit. Store the result in a variable called affordable_fruits
Ruby
Need a hint?

Use reject { |fruit, price| price >= price_limit } to remove expensive fruits.

4
Print the affordable fruits
Write puts affordable_fruits to print the resulting hash of affordable fruits
Ruby
Need a hint?

Use puts affordable_fruits to show the filtered fruits.