Tom D
Tom D

Reputation: 361

'empty?' returns 'false' for an empty array within an empty array

My script returns an empty array in another empty array (or I guess it's not empty because there is another empty array within the other array).

I have an if condition that checks if the outer array is empty. It says it's not empty because it contains an empty array. I need help to get it return false. I believe the best method would be to check if the inner array is empty, but I'm not sure where the inner array is created.

Here's the code for the method that checks if the array is empty:

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 

The directory_list returns [[]], and empty? returns false.

I have another method that stores items into an array, but I cannot figure out why there is an array within an array:

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

I'm passing an array of directories into the store_directories method.

Upvotes: 0

Views: 90

Answers (1)

Gagan Gami
Gagan Gami

Reputation: 10251

The directory_list returns [[]], and empty? returns false.

it's working properly and it returns correct value, as your directory_list is not empty array, it contain empty array. You need to use Array#flatten

> [[]].flatten.empty?
#=> true 

Upvotes: 1

Related Questions