0
0
Rubyprogramming~15 mins

How Ruby interprets code at runtime - Try It Yourself

Choose your learning style9 modes available
How Ruby interprets code at runtime
📖 Scenario: Imagine you are writing a simple Ruby program that calculates the total price of items in a shopping cart. Ruby reads and runs your code line by line, interpreting it at runtime. This project will help you understand how Ruby processes your code step by step.
🎯 Goal: You will create a Ruby program that stores item prices, sets a tax rate, calculates the total price including tax, and then prints the final amount. This will show how Ruby interprets variables, calculations, and output commands at runtime.
📋 What You'll Learn
Create a hash with exact item names and prices
Create a variable for the tax rate
Calculate the total price including tax using Ruby code
Print the final total price
💡 Why This Matters
🌍 Real World
Calculating totals with tax is common in shopping apps and billing systems.
💼 Career
Understanding how Ruby interprets code helps you write clear, correct programs for real-world tasks.
Progress0 / 4 steps
1
Create the item prices hash
Create a hash called items with these exact entries: 'apple' => 2.0, 'banana' => 1.5, 'orange' => 3.0
Ruby
Need a hint?

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

2
Set the tax rate
Create a variable called tax_rate and set it to 0.08 (which means 8% tax)
Ruby
Need a hint?

Just assign the number 0.08 to the variable tax_rate.

3
Calculate total price including tax
Create a variable called total_price that sums all item prices from items and adds tax using tax_rate
Ruby
Need a hint?

Use items.values.sum to add prices, then multiply by 1 + tax_rate to add tax.

4
Print the total price
Write a puts statement to print the total_price rounded to 2 decimal places using round(2)
Ruby
Need a hint?

Use puts total_price.round(2) to print the result with two decimals.