E-commerce companies receive daily exports from Shopify/Square and need automated monthly reports for management. This drill teaches you to parse sales CSV data, calculate monthly revenue by category, identify top-selling products, and generate summary reports with totals, averages, and percentage breakdowns. You'll master CSV processing, hash aggregation operations, and business metric calculations. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.
Parse CSV manually: line.split(',')
Date.parse(string).strftime('%Y-%m') extracts year-month
Calculate revenue per transaction: quantity.to_i * unit_price.to_f
Use a hash to accumulate totals: hash[key] ||= 0; hash[key] += value
max_by { |k, v| v } finds the entry with maximum value
Format currency: num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse
analyze_sales('sales.csv')
Total revenue: $4,375 Best month: 2024-01 ($2,875) Top category: Electronics (92.0%)
analyze_sales('sales.csv')
puts 'Month verified'
Total revenue: $4,375 Best month: 2024-01 ($2,875) Top category: Electronics (92.0%) Month verified
analyze_sales('sales.csv')
puts 'Category verified'
Total revenue: $4,375 Best month: 2024-01 ($2,875) Top category: Electronics (92.0%) Category verified
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept