0
0
Rubyprogramming~30 mins

Debugging with pry and byebug in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Debugging with pry and byebug
📖 Scenario: You are working on a simple Ruby program that calculates the total price of items in a shopping cart. Sometimes, the total price is not what you expect. To find the problem, you will use debugging tools pry and byebug to pause the program and check values step-by-step.
🎯 Goal: Learn how to use pry and byebug to debug a Ruby program by inserting breakpoints and inspecting variables.
📋 What You'll Learn
Create a hash called cart with specific items and prices
Add a variable discount_threshold to set a price limit
Use pry or byebug to pause the program inside a loop
Print the final total price after applying discount logic
💡 Why This Matters
🌍 Real World
Debugging tools like <code>pry</code> and <code>byebug</code> help developers find and fix problems in their code by letting them pause and check values step-by-step.
💼 Career
Knowing how to use debugging tools is essential for software developers to efficiently troubleshoot and improve code quality.
Progress0 / 4 steps
1
DATA SETUP: Create the shopping cart hash
Create a hash called cart with these exact entries: 'apple' => 2.5, 'banana' => 1.2, 'chocolate' => 3.0, 'bread' => 2.0
Ruby
Need a hint?

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

2
CONFIGURATION: Set the discount threshold
Create a variable called discount_threshold and set it to 5.0
Ruby
Need a hint?

Just assign the number 5.0 to the variable discount_threshold.

3
CORE LOGIC: Use byebug to debug the total calculation
Add require 'byebug' at the top. Then, create a variable total set to 0. Use a for loop with variables item and price to iterate over cart. Inside the loop, add byebug to pause execution. Add price to total only if price is less than discount_threshold.
Ruby
Need a hint?

Use require 'byebug' to load the debugger. Insert byebug inside the loop to pause and inspect variables.

4
OUTPUT: Print the total price
Add a puts statement to print the string "Total price: " followed by the value of total
Ruby
Need a hint?

Use puts with string interpolation to show the total price.