class DirectorySearcher
in directory names, file names, and contents of files within given paths.
This class provides methods to search for a specified pattern
Class DirectorySearcher
def find_directory_names
- 
        (Array- List of matching directory names.) 
def find_directory_names match_dirs = [] @paths.each do |path| Find.find(path) do |p| # Find.prune unless @include_subdirectories || path == p match_dirs << p if File.directory?(p) && p.match?(@pattern) end end match_dirs end
def find_file_contents
- 
        (Hash)- A hash where each key is a file path and each value is an array of hashes with :line_number and :line keys.
def find_file_contents match_details = {} @paths.each do |path| Find.find(path) do |p| Find.prune unless @include_subdirectories || path == p next unless File.file?(p) next if @filename_glob && !File.fnmatch(@filename_glob, File.basename(p)) begin File.foreach(p).with_index(1) do |line, line_num| # Index starts from 1 for line numbers line_utf8 = line.encode('UTF-8', invalid: :replace, undef: :replace, replace: '') if line_utf8.match?(@pattern) match_details[p] ||= [] # match_details[p] << { number: line_num, line: line_utf8.chomp } match_details[p] << IndexedLine.new(line_num, line_utf8.chomp) end end rescue EncodingError # Optionally log the file with encoding issues # puts "Encoding error in file: #{p}" end end end match_details end
def find_file_names
- 
        (Array- List of matching file names.) 
def find_file_names match_files = [] @paths.each do |path| Find.find(path) do |p| # Find.prune unless @include_subdirectories || path == p next unless File.file?(p) file_name = File.basename(p) next if @filename_glob && !File.fnmatch(@filename_glob, file_name) begin match_files << p if file_name.encode('UTF-8', invalid: :replace, undef: :replace, replace: '').match?(@pattern) rescue EncodingError # Optionally log the file with encoding issues # puts "Encoding error in file: #{p}" end end end match_files end
def initialize(pattern, paths, include_subdirectories: true, filename_glob: '*.[Mm][Dd]') #'*.md'
- 
        filename_glob(String, nil) -- Glob pattern for file names.
- 
        include_subdirectories(Boolean) -- Whether to search in subdirectories.
- 
        paths(Array) -- List of directories to search in.
- 
        pattern(Regexp) -- The regular expression pattern to search for.
def initialize(pattern, paths, include_subdirectories: true, filename_glob: '*.[Mm][Dd]') #'*.md' @pattern = Regexp.new(pattern, Regexp::IGNORECASE) @paths = paths @include_subdirectories = include_subdirectories @filename_glob = filename_glob end