我寫了下面的腳本通過目錄遍歷,它的子目錄和它們的文件:紅寶石:通過DIR的和文件遍歷
def self.search_files_in_dir_for(dir, strings)
results = {}
puts "Initializing test ..."
Dir.foreach(dir) do |file|
next if file == '.' or file == '..'
puts 'TEST: Testing dir "' + dir + "/" + file + '"'
if File.file?(file)
puts 'TEST: Testing "' + dir + "/" + file + '"'
line_number = 0
IO.foreach(file) do |line|
line_number = line_number + 1
if strings.any? { |string| line.include?(string) }
string = strings.detect { |string| line.include?(string) }
puts source = file + ":" + line_number.to_s
results[source] = string
end
end
elsif File.directory?(file)
# Search child directories
search_files_in_dir_for(File.join(dir, file), strings)
end
end
end
鑑於followiing目錄+文件結構:
- views
- application
- abcdefg
- _partial2.html.erb
- _partial1.html.erb
- layouts
- application.html.erb
和當通過views
目錄的路徑爲dir
時,我得到該輸出:
Initializing test ...
TEST: Testing dir "views/application"
TEST: Testing dir "views/layouts"
我希望是這樣的:
Initializing test ...
TEST: Testing dir "views/application"
TEST: Testing dir "views/application/abcdefg"
TEST: Testing "views/application/abcdefg/_partial2.html.erb"
TEST: Testing "views/application/_partial1.html.erb"
TEST: Testing dir "views/layouts"
TEST: Testing "views/layouts/application.html.erb"
我是什麼在腳本做錯了什麼?
你熟悉'Dir.glob(「./**/*」)',這將給你所有的目錄和文件的列表? –
@maxple no ..... – jonhue