gpt4 book ai didi

ruby - 哈希迭代不返回子目录内容

转载 作者:太空宇宙 更新时间:2023-11-03 16:41:51 25 4
gpt4 key购买 nike

我有一个方法可以查找给定父目录的子目录。我将父目录存储在哈希中,然后将哈希作为参数传入。我试图将子目录的内容收集到一个数组中,然后将其输出到报告中。

我遇到了一个问题,数组的内容只存储散列的最后一个值中的目录,我很快意识到内容在循环的每次迭代中都被覆盖了,所以我创建了一个新数组来存储每次迭代后的内容。

在我更改之前,我看到子目录正常输出。这是我的代码示例:

# stores subdirectory contents into an array
def store_directories(directories, folder_to_exclude)
# changes working directory to the directory variable

subdir_list = nil
directories.each_value do |value|

directory = "#{value}"

puts Time.now.to_s + " > " + "Updating search directory..."
Dir.chdir(directory)

# outputs only subdirectories with a creation date of older than 24 hours, except for folders names 'test'
Dir.glob("*.*").map(&File.method(:realpath))
puts Time.now.to_s + " > " + "Gathering subdirectories..."

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 subdir_list

end
return subdir_list
end

输出看起来像这样:

/path/to/parent/subdirectory1
/path/to/parent/subdirectory2
/path/to/parent/subdirectory3
/path/to/parent/subdirectory4 ...

除了我只获取最后一个目录的内容外,这几乎按照我想要的方式工作。

这是我进行更改后的代码示例,以便数组的内容不会被覆盖:

# stores subdirectory contents into an array
def store_directories(directories, folder_to_exclude)
# changes working directory to the directory variable

subdir_list = directories.each_value do |value|

directory = "#{value}"

puts Time.now.to_s + " > " + "Updating search directory..."
Dir.chdir(directory)

# outputs only subdirectories with a creation date of older than 24 hours, except for folders names 'test'
Dir.glob("*.*").map(&File.method(:realpath))
puts Time.now.to_s + " > " + "Gathering subdirectories..."

subdirectories = 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 subdirectories

end
return subdir_list
end

但这就是问题所在,现在我的输出看起来像这样......

directory1
/path/to/parent1/
directory2
/path/to/parent2/
directory3
/path/to/parent3/

突然间,我的代码不再寻找子目录的内容,而是只返回父目录...

最佳答案

试试这个:

# stores subdirectory contents into an array
def store_directories(directories, folder_to_exclude)
# changes working directory to the directory variable

subdir_list = nil
directories.each_value do |value|

directory = "#{value}"

puts Time.now.to_s + " > " + "Updating search directory..."
Dir.chdir(directory)

# outputs only subdirectories with a creation date of older than 24 hours, except for folders names 'test'
Dir.glob("*.*").map(&File.method(:realpath))
puts Time.now.to_s + " > " + "Gathering subdirectories..."

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 subdir_list

end
return subdir_list
end

这基本上会保留您的旧代码,并在每次迭代时将新项目附加到 subdir_list,而不是覆盖。请注意,这里唯一真正的变化是 subdir_list =改为subdir_list <<

关于ruby - 哈希迭代不返回子目录内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46967332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com