0
0
Rubyprogramming~15 mins

Map/collect for transformation in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Map/collect for transformation
📖 Scenario: You work at a small bakery. You have a list of prices for different pastries. You want to create a new list that shows the prices with tax added.
🎯 Goal: Build a Ruby program that uses map (also called collect) to add tax to each price in a list.
📋 What You'll Learn
Create an array called prices with the exact values: 2.5, 3.0, 4.75, 1.25
Create a variable called tax_rate and set it to 0.1 (which means 10% tax)
Use map on prices with block variable price to create a new array prices_with_tax where each price is increased by tax
Print the prices_with_tax array
💡 Why This Matters
🌍 Real World
Adding tax to prices is a common task in stores and online shops to show customers the final cost.
💼 Career
Understanding how to transform lists of data with <code>map</code> is a key skill for Ruby developers working with collections.
Progress0 / 4 steps
1
Create the initial prices array
Create an array called prices with these exact values: 2.5, 3.0, 4.75, 1.25
Ruby
Need a hint?

Use square brackets [] to create an array and separate values with commas.

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

Just assign 0.1 to the variable tax_rate.

3
Use map to add tax to each price
Use map on the prices array with block variable price to create a new array called prices_with_tax. Each element should be the original price plus tax (price + price * tax_rate).
Ruby
Need a hint?

Use prices.map { |price| ... } and inside the block add tax to each price.

4
Print the prices with tax
Write a puts statement to print the prices_with_tax array.
Ruby
Need a hint?

Use puts prices_with_tax to print each price on its own line.