我試圖將目錄中的文件與其他目錄中的其他文件及其子目錄中的文件匹配使用ruby。bsearch與ruby之間的區別
我嘗試使用這個文件的架構做一個小試驗:
tree .
.
├── src
│ ├── lol
│ │ └── toto
│ └── lolilolpouet
│ └── tutu
│ └── tata
├── test
│ ├── tata
│ └── toto
└── test.rb
我的Ruby代碼是:
require 'find'
src_dir_files = []
Find.find('./src') do |file|
src_dir_files << file
puts "found #{file}"
end
Dir.foreach('./test') do |file|
next if file == '.' or file == '..'
puts "search for /#{file}"
res = src_dir_files.bsearch{|s| s.end_with? "/#{file}"}
puts "Found :#{res}"
end
輸出是:
found ./src
found ./src/lol
found ./src/lol/toto
found ./src/lolilolpouet
found ./src/lolilolpouet/tutu
found ./src/lolilolpouet/tutu/tata
search for /tata
Found :./src/lolilolpouet/tutu/tata
search for /toto
Found :
託託搜索不會返回結果。 任何想法爲什麼,以及如何解決它?
編輯:如果我用find來替換bsearch,上面的代碼表現得如預期。 任何人都可以向我解釋這兩種方法之間的區別嗎?
我上傳了一個TGZ,如果你想嘗試一下:
你似乎已經改變了原來的問題 – bjhaid