Given a hash that contains function names like “find_by_user”, “find_by_id”, …
I want to search in a directory of files, and return a object that has each file name, along with the line numbers of where the function name occurred.
I have this so far:
files = Dir.glob(@folder_path)
files.each do |file_name|
content = File.read(file_name)
end
This will be scanning a few hundred files.
Here’s the basic functionality you need:
You can choose to use this as you like, iterating over multiple files and/or patterns, or using
Regexp.unionto create a pattern matching any one of a set of strings.However: this is what
grepwas made for:In Ruby you could call this code and get the output you want via:
Grep can also recurse into directories, match only particular file names, take regular expressions as patterns, and more.