0
0
Rubyprogramming~15 mins

Comparison operators in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Comparison operators
📖 Scenario: You are helping a store manager compare product prices to decide which items are cheaper or more expensive.
🎯 Goal: You will write a Ruby program that uses comparison operators to compare product prices and print the results.
📋 What You'll Learn
Create a hash with product names and their prices
Create a variable with a price threshold
Use comparison operators to check if each product price is less than, equal to, or greater than the threshold
Print the comparison results
💡 Why This Matters
🌍 Real World
Stores often compare product prices to decide discounts or promotions.
💼 Career
Understanding comparison operators is essential for decision-making logic in programming jobs.
Progress0 / 4 steps
1
Create the product prices hash
Create a hash called products with these exact entries: 'apple' => 100, 'banana' => 50, 'cherry' => 75
Ruby
Need a hint?

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

2
Set the price threshold
Create a variable called threshold and set it to 70
Ruby
Need a hint?

Just assign the number 70 to the variable threshold.

3
Compare product prices to the threshold
Use a products.each loop with variables product and price to compare each price with threshold using <, ==, and > operators. Inside the loop, create a variable result that stores the string "less than" if price < threshold, "equal to" if price == threshold, or "greater than" if price > threshold.
Ruby
Need a hint?

Use if, elsif, and else inside the loop to set result.

4
Print the comparison results
Inside the products.each loop, add a puts statement to print the message: "The price of #{product} is #{result} the threshold."
Ruby
Need a hint?

Use puts with string interpolation to print the message inside the loop.