无法将数组转换为字符串(Ruby)
问题描述:
我遇到了此代码(再次)的问题。 我试图让Ruby来检查tree
是否等于$do_not_scan
中的任何项目,我所得到的全部是“cannot convert Array into String
”错误。任何方式来解决这样的事情?无法将数组转换为字符串(Ruby)
我的代码:
#filesniffer by Touka, ©2015
$filecount = 0
$trees = Dir.entries("C:\\")
$do_not_scan = ["bootmgr", "BOOTNXT", "USER", ".", "Documents and Settings", "PerfLogs", "System Recovery", "System Volume Information", "$RECYCLE.BIN"]
def scan
$trees.each do |tree|
unless tree.include?($do_not_scan[0...8])
puts "scanning #{tree}"
entries = Dir.entries("c:\\#{tree}")
entries.each do |filename|
if filename == "**\*.dll"
puts "Found #{filename} in #{tree}"
end
end
end
end
end
def scan_loop
$trees.each do |tree|
scan
unless tree.include?($do_not_scan[0...8])
subtrees = Dir.entries("#{tree}")
subtrees.each do |tree|
scan
scan_loop
end
end
end
end
scan_loop
sleep
答
它看起来像以下必须在scan
和方法来改变:
$do_not_scan[0...8].include?(tree)
代替关:
tree.include?($do_not_scan[0...8])
+0
这是朝着正确方向迈出的一步,但它现在试图扫描文件。 (需要上课,所以我会稍后再回来) – Touka
+0
抱歉,但我没有得到你的观点。 –
我会用一个正则表达式代替。这将帮助我避免文件夹名称区分大小写的比较问题。此外,我会避免递归,它使用更多的资源,在这种情况下不需要它。 – Myst
您还在不更新'$ trees'的情况下调用扫描。您可以通过将'#scan'和'#scan_loop'方法作为参数传递给全局变量'$ trees'来解决方法 – jphager2
备注 - 请停止使用'$ global'变量。他们只是不使用红宝石。正常变量不需要'$' – DGM