2017-11-08 1 views
0

내 스크립트는 다른 빈 배열에 빈 배열을 반환합니다 (또는 다른 배열에 다른 빈 배열이 있기 때문에 비어 있지 않습니다).'empty?' 빈 배열 내에서 빈 배열에 대해 'false'를 반환합니다.

외부 배열이 비어 있는지 확인하는 조건은 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 

directory_list 반환 [[]]empty? 반환 false.

나는 배열에 항목을 저장하는 다른 방법을 가지고 있지만, 배열에서 배열이 이유를 알아낼 수 없습니다 : 나는 store_directories 방법으로 디렉토리의 배열을 전달하고있어

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 

가.

답변

1

directory_list[[]]이고, empty?false입니다.

올바르게 작동하고 올바른 값을 반환합니다. directory_list는 빈 배열이 아니므로 빈 배열이 포함되어 있습니다. 사용해야합니다. Array#flatten

관련 문제