0
0
Rubyprogramming~30 mins

CSV library basics in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
CSV library basics
📖 Scenario: You work in a small bookstore. You have a CSV file listing books and their prices. You want to read this file and find books cheaper than a certain price.
🎯 Goal: Build a Ruby program that reads a CSV file of books and prices, filters books cheaper than a set price, and prints their titles.
📋 What You'll Learn
Use Ruby's CSV library to read data
Create a variable for the price limit
Use a loop to filter books cheaper than the limit
Print the titles of the filtered books
💡 Why This Matters
🌍 Real World
Reading and filtering CSV data is common in many jobs like sales, inventory, and data analysis.
💼 Career
Knowing how to use Ruby's CSV library helps you handle real data files and automate tasks in many programming jobs.
Progress0 / 4 steps
1
Create the CSV data as a string
Create a variable called csv_data and set it to this exact string including newlines: "Title,Price\nRuby Basics,25\nJavaScript Guide,30\nPython Intro,20"
Ruby
Need a hint?

Use double quotes and include newline characters \n exactly as shown.

2
Set the price limit
Create a variable called price_limit and set it to the integer 25
Ruby
Need a hint?

Just assign the number 25 to price_limit.

3
Parse CSV and filter books cheaper than price_limit
Use require 'csv' to load the CSV library. Then create a variable called cheap_books that uses CSV.parse on csv_data with headers: true. Use .select with a block that keeps rows where the Price converted to integer is less than price_limit. Then map the result to get an array of the Title values.
Ruby
Need a hint?

Use CSV.parse with headers: true, then select rows with price less than price_limit, then map to get titles.

4
Print the titles of cheap books
Write a puts statement to print the cheap_books array.
Ruby
Need a hint?

Use puts to print the array. It will print each title on its own line.