Understanding disk usage is crucial for system administration and application optimization. This drill teaches you to calculate file sizes, filter by file types, and format output for human readability. You'll learn data aggregation, grouping by extension, and size formatting—essential skills for building DevOps tools, backup systems, and disk usage analyzers.
JSON.parse(File.read(file)) loads the metadata
File.extname(path) returns extension including the dot (e.g., '.txt')
Empty extension means file has no extension
Hash.new { |h, k| h[k] = { size: 0, count: 0 } } creates nested default hash
Sort by size descending: array.sort_by { |k, v| -v[:size] }
1024 bytes = 1 KB, 1024 KB = 1 MB, 1024 MB = 1 GB
analyze_directory('metadata.json')
DIRECTORY SIZE ANALYSIS Directory: /project ================================================== BY FILE TYPE .md: 7.00 KB (2 files) .json: 5.50 KB (2 files) .rb: 3.00 KB (2 files) No extension: 1.32 KB (2 files) -------------------------------------------------- SUMMARY Total Files: 8 Total Size: 16.82 KB
puts format_size(500)
500 bytes
puts format_size(2048)
2.00 KB
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept