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.
JSON.parse(File.read(file)) loads JSON data
string.scan(pattern).length counts occurrences
Use a hash to track file path and count
Sort files alphabetically: array.sort_by { |f| f[:path] }
Check count > 0 to determine if file was modified
Sum counts for total replacements
find_and_replace('config.json', 'files.json')
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
find_and_replace('config.json', 'files.json')
puts 'Counts OK'
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
find_and_replace('config.json', 'files.json')
puts 'Skip OK'
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
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept