📖 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
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
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
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
Hint
Use puts total_price.round(2) to print the result with two decimals.
Practice
(1/5)
1. What does it mean that Ruby interprets code at runtime?
easy
A. Ruby only checks code syntax but does not run it.
B. Ruby converts all code to machine language before running.
C. Ruby runs code only after saving it to a file.
D. Ruby reads and runs code line by line as the program runs.
Solution
Step 1: Understand interpretation
Interpreting means reading and executing code step by step during program execution.
Step 2: Compare options
The option "Ruby reads and runs code line by line as the program runs." describes this process correctly; others describe compiling or partial actions.
Final Answer:
Ruby reads and runs code line by line as the program runs. -> Option D
Quick Check:
Interpretation = line-by-line execution [OK]
Hint: Interpret means run code step-by-step as you go [OK]
Common Mistakes:
Confusing interpretation with compilation
Thinking Ruby only checks syntax
Believing Ruby runs code after saving only
2. Which of the following is the correct way to print "Hello" in Ruby?
easy
A. echo "Hello"
B. console.log("Hello")
C. print("Hello")
D. printf("Hello %d")
Solution
Step 1: Identify Ruby print syntax
Ruby uses the method print or puts to output text; print("Hello") is valid.
Step 2: Check other options
echo is PHP, console.log is JavaScript, printf("Hello %d") is C-style but errors due to missing argument for %d.
Final Answer:
print("Hello") -> Option C
Quick Check:
Ruby print method = print("Hello") [OK]
Hint: Use print or puts to show text in Ruby [OK]
Common Mistakes:
Using JavaScript or PHP print commands
Missing parentheses in print
Confusing printf with print
3. What will be the output of this Ruby code?
count = 0
3.times do
count += 1
end
print count
medium
A. 3
B. 0
C. 1
D. Error
Solution
Step 1: Understand the times loop
The times method runs the block 3 times, increasing count by 1 each time.
Step 2: Calculate final count
Starting from 0, after 3 increments, count becomes 3.
Final Answer:
3 -> Option A
Quick Check:
0 + 1 + 1 + 1 = 3 [OK]
Hint: times runs block that many times, increment count each time [OK]
Common Mistakes:
Thinking count stays 0
Confusing times with for loop syntax
Expecting error due to block
4. Find the error in this Ruby code:
def greet
puts "Hello"
end
greet(1)
medium
A. ArgumentError: wrong number of arguments
B. No error, prints Hello
C. SyntaxError due to missing parentheses
D. NameError: greet not defined
Solution
Step 1: Check method definition
greet is defined without parameters, so it expects zero arguments.
Step 2: Check method call
Calling greet(1) passes one argument, causing ArgumentError.
Final Answer:
ArgumentError: wrong number of arguments -> Option A
Quick Check:
Method params mismatch = ArgumentError [OK]
Hint: Method defined with no params, calling with one causes error [OK]
Common Mistakes:
Thinking Ruby ignores extra arguments
Confusing syntax error with argument error
Assuming greet is undefined
5. Given this Ruby code:
def add(x, y)
x + y
end
result = add(2, 3) + add(4, 5)
print result
How does Ruby interpret and calculate the final output?
hard
A. It prints the string 'add(2, 3)add(4, 5)'
B. It adds 2 and 3, then adds 4 and 5, sums both results to print 14
C. It throws an error because add is called twice
D. It adds 2 and 3, ignores second add call, prints 5
Solution
Step 1: Evaluate first add call
add(2, 3) returns 5 by adding 2 + 3.
Step 2: Evaluate second add call and sum
add(4, 5) returns 9; sum 5 + 9 = 14.
Step 3: Print the result
print outputs 14 as the final result.
Final Answer:
14 -> Option B
Quick Check:
5 + 9 = 14 [OK]
Hint: Each method call runs fully, then sums results [OK]
Common Mistakes:
Thinking only first add runs
Expecting string concatenation instead of addition
Assuming error on multiple calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.