Challenge

Problem

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.

Difficulty: Intermediate

Instructions

  1. Read file metadata from metadata.json (contains path and size_bytes for each file)
  2. Calculate total size in bytes
  3. Group files by extension and calculate size per extension
  4. Sort extensions by size (largest first)
  5. Format output with proper size units (bytes, KB, MB, GB)
  6. Handle files without extensions as 'No extension'
  7. Print formatted report with file type breakdown and summary

Files

Editable
Read-only

Hints

Hint 1

JSON.parse(File.read(file)) loads the metadata

Hint 2

File.extname(path) returns extension including the dot (e.g., '.txt')

Hint 3

Empty extension means file has no extension

Hint 4

Hash.new { |h, k| h[k] = { size: 0, count: 0 } } creates nested default hash

Hint 5

Sort by size descending: array.sort_by { |k, v| -v[:size] }

Hint 6

1024 bytes = 1 KB, 1024 KB = 1 MB, 1024 MB = 1 GB

Ruby 3.4

Provided Files (Read-only)

1. Correct directory analysis

Input:
analyze_directory('metadata.json')
Expected Output:
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

2. format_size works for bytes

Input:
puts format_size(500)
Expected Output:
500 bytes

3. format_size works for KB

Input:
puts format_size(2048)
Expected Output:
2.00 KB
+ 2 hidden test cases