Business analytics often requires aggregating time-series data into meaningful periods for reporting and decision-making. This drill teaches you to parse dates, group transactions by month, calculate aggregate statistics, and format currency. You'll learn date manipulation, hash-based grouping, aggregation functions, and business reporting—essential skills for building dashboards, analytics tools, and financial reports. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.
Date.parse(string) converts date string to Date object
date.strftime('%Y-%m') formats date as 'YYYY-MM'
Hash.new { |h, k| h[k] = [] } creates hash with array default values
array.sum calculates total of all elements
hash.keys.sort sorts month strings chronologically
hash.values.flatten combines all arrays into one
Format decimals: '%.2f' % number
Add commas: reverse, gsub, reverse
analyze_sales('sales.csv')
MONTHLY SALES REPORT ================================================== Month: 2024-01 Transactions: 3 Total Sales: $1,419.97 Average: $473.32 Month: 2024-02 Transactions: 3 Total Sales: $1,779.97 Average: $593.32 Month: 2024-03 Transactions: 4 Total Sales: $1,869.96 Average: $467.49 -------------------------------------------------- OVERALL SUMMARY Total Transactions: 10 Total Sales: $5,069.90 Average per Transaction: $506.99
puts format_currency(1234.56) puts format_currency(99.90) puts format_currency(10000.00)
$1,234.56 $99.90 $10,000.00
analyze_sales('sales.csv')
puts 'Sorted OK'
MONTHLY SALES REPORT ================================================== Month: 2024-01 Transactions: 3 Total Sales: $1,419.97 Average: $473.32 Month: 2024-02 Transactions: 3 Total Sales: $1,779.97 Average: $593.32 Month: 2024-03 Transactions: 4 Total Sales: $1,869.96 Average: $467.49 -------------------------------------------------- OVERALL SUMMARY Total Transactions: 10 Total Sales: $5,069.90 Average per Transaction: $506.99 Sorted OK
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept