Arrays ';空?';返回';假';对于空数组中的空数组
我的脚本在另一个空数组中返回一个空数组(或者我猜它不是空的,因为另一个数组中有另一个空数组) 我有一个Arrays ';空?';返回';假';对于空数组中的空数组,arrays,ruby,Arrays,Ruby,我的脚本在另一个空数组中返回一个空数组(或者我猜它不是空的,因为另一个数组中有另一个空数组) 我有一个if条件来检查外部数组是否为空。它说它不是空的,因为它包含一个空数组。我需要帮助才能让它返回false。我相信最好的方法是检查内部数组是否为空,但我不确定内部数组是在哪里创建的 下面是检查数组是否为空的方法的代码: def directory_check(directory_list, save_to_file, today_date, output_file, output_extension
if
条件来检查外部数组是否为空。它说它不是空的,因为它包含一个空数组。我需要帮助才能让它返回false
。我相信最好的方法是检查内部数组是否为空,但我不确定内部数组是在哪里创建的
下面是检查数组是否为空的方法的代码:
def directory_check(directory_list, save_to_file, today_date, output_file, output_extension, results_file_output)
if directory_list.empty? == false
# changes the working directory to the file output directory for the file
Dir.chdir(save_to_file)
# writes the array contents into a new file
file_name = output_file + "_" + today_date + output_extension
puts Time.now.to_s + " > " + "Saving contents to: " + results_file_output + file_name
puts ""
File.open(file_name, "a+") do |f|
directory_list.each { |element| f.puts(element) }
end
else
puts Time.now.to_s + " > " + "This directory does not contain any subdirectories that are older than 24 hours"
exit
end
end
目录列表
返回[[]]
,为空?
返回false
我有另一种方法将项目存储到数组中,但我无法理解为什么数组中有一个数组:
def store_directories(directories, folder_to_exclude)
# updates search directory for each value for the directories hash
subdir_list = Array.new
directories.each do |element|
directory = "#{element}"
puts Time.now.to_s + " > " + "Updating search directory: " + directory
Dir.chdir(directory)
# outputs only subdirectories with a creation date of older than 24 hours, except for folders names 'Archive'
Dir.glob("*.*").map(&File.method(:realpath))
puts Time.now.to_s + " > " + "Gathering subdirectories..."
puts ""
# Stores the contents of the query into an array and appends to the list for each iteration of the array
subdir_list << Dir.glob("*").map(&File.method(:realpath)).reject {|files|
(not File.directory?(files) &&
(File.mtime(files) < (Time.now - (60*1440))) &&
(not files == directory + folder_to_exclude))
}
puts ""
puts "Adding new folders to the list..."
puts ""
puts "Excluding: " + directory + folder_to_exclude
puts ""
puts subdir_list
puts " "
end
return subdir_list
end
def存储目录(目录、要排除的文件夹)
#为目录哈希的每个值更新搜索目录
子目录列表=Array.new
目录。每个do |元素|
directory=“#{element}”
将Time.now.to_+“>”+“更新搜索目录:“+目录”
Dir.chdir(目录)
#仅输出创建日期早于24小时的子目录,文件夹名称“Archive”除外
Dir.glob(“*.”).map(&File.method(:realpath))
将Time.now.to_+“>”+“正在收集子目录…”
放置“”
#将查询的内容存储到一个数组中,并为数组的每次迭代追加到列表中
子目录列表目录列表
返回[[]]
,为空
返回为假
它工作正常并返回正确的值,因为目录列表不是空数组,它包含空数组。您需要使用
> [[]].flatten.empty?
#=> true