Challenge

Problem

Batch text processing is essential for refactoring, configuration updates, and code migrations. This drill teaches you to search multiple files for patterns, perform replacements, and generate reports. You'll learn string pattern matching, counting occurrences, and batch processing—essential skills for building developer tools, automation scripts, and migration utilities.

Difficulty: Intermediate

Instructions

  1. Read configuration from config.json:
    • search: the pattern to find
    • replace: the replacement text
    • file_pattern: the type of files (for display)
  2. Read files data from files.json (simulates file contents)
  3. For each file:
    • Search for pattern
    • Count occurrences
    • Simulate replacement
  4. Print formatted report with:
    • Configuration used
    • Modified files with replacement counts
    • Summary statistics

Files

Editable
Read-only

Hints

Hint 1

JSON.parse(File.read(file)) loads JSON data

Hint 2

string.scan(pattern).length counts occurrences

Hint 3

Use a hash to track file path and count

Hint 4

Sort files alphabetically: array.sort_by { |f| f[:path] }

Hint 5

Check count > 0 to determine if file was modified

Hint 6

Sum counts for total replacements

Ruby 3.4

Provided Files (Read-only)

1. Correct find and replace report

Input:
find_and_replace('config.json', 'files.json')
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: TODO
Replace: FIXME
Files: *.txt
--------------------------------------------------
Modified: file1.txt (2 replacements)
Modified: subfolder/file3.txt (2 replacements)
--------------------------------------------------
SUMMARY
Files scanned: 3
Files modified: 2
Total replacements: 4

2. Counts occurrences correctly

Input:
find_and_replace('config.json', 'files.json')
puts 'Counts OK'
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: TODO
Replace: FIXME
Files: *.txt
--------------------------------------------------
Modified: file1.txt (2 replacements)
Modified: subfolder/file3.txt (2 replacements)
--------------------------------------------------
SUMMARY
Files scanned: 3
Files modified: 2
Total replacements: 4
Counts OK

3. Skips files without matches

Input:
find_and_replace('config.json', 'files.json')
puts 'Skip OK'
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: TODO
Replace: FIXME
Files: *.txt
--------------------------------------------------
Modified: file1.txt (2 replacements)
Modified: subfolder/file3.txt (2 replacements)
--------------------------------------------------
SUMMARY
Files scanned: 3
Files modified: 2
Total replacements: 4
Skip OK
+ 2 hidden test cases