Skip to content

Instantly share code, notes, and snippets.

@Luigi123
Created June 9, 2016 18:56
Show Gist options
  • Save Luigi123/1ac3021750f1760c08377ac3f37baf31 to your computer and use it in GitHub Desktop.
Save Luigi123/1ac3021750f1760c08377ac3f37baf31 to your computer and use it in GitHub Desktop.
class Search
def initialize(search_str, base_dir, search_depth)
@search_str = search_str
@base_dir = base_dir
@search_depth = search_depth
end
def do_search
search_dir(@base_dir, @search_depth)
end
private
def add?(full_path)
file_name = File.basename(full_path)
file_name.include?(@search_str)
end
def search_dir(current_dir, depth)
result = []
return result if depth == 0
Dir[current_dir + '/*'].each do |current_file|
if File.directory?(current_file)
children = search_dir(current_file, depth - 1)
result.concat(children)
else
result.push(current_file) if add?(current_file)
end
end
result
end
end
search = Search.new('Directory', 'Filename', 5)
files = search.do_search
files.each {|file| puts file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment