0
0
Rubyprogramming~15 mins

Select/filter for filtering in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Select/filter for filtering
📖 Scenario: You are managing a small bookstore's inventory. You want to find all books that are priced below a certain amount to offer a discount.
🎯 Goal: Build a Ruby program that filters a list of books to find only those priced below a set threshold using the select method.
📋 What You'll Learn
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'JavaScript Guide' => 40, 'Python 101' => 30, 'HTML & CSS' => 20
Create a variable called max_price and set it to 30
Use select on books to create a new hash called affordable_books containing only books with price less than max_price
Print affordable_books to display the filtered books
💡 Why This Matters
🌍 Real World
Filtering items by price is common in online stores to show discounts or deals.
💼 Career
Understanding how to filter collections is important for data processing and building user-friendly applications.
Progress0 / 4 steps
1
Create the initial books hash
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'JavaScript Guide' => 40, 'Python 101' => 30, 'HTML & CSS' => 20
Ruby
Need a hint?
Use curly braces { } to create a hash with keys and values separated by =>
2
Set the maximum price threshold
Create a variable called max_price and set it to 30
Ruby
Need a hint?
Use a simple assignment to create max_price with value 30
3
Filter books priced below max_price
Use select on books to create a new hash called affordable_books containing only books with price less than max_price
Ruby
Need a hint?
Use a block with select that checks if price is less than max_price
4
Print the filtered affordable books
Write puts affordable_books to display the filtered books
Ruby
Need a hint?
Use puts to print the affordable_books hash