0

I am trying to come up with a mechanism that will check whether a list of known directories exist before creating them on the system.
To do so, I wrote the following; but it does not really work. I believe there must be an issue with the each loop but I could not fix it on my own.
I would like to make sure that none of the directory in the array exist before creating.

Many thanks for your help!

method_options path: :string
def create_folders
  if profile_folders_exist?
    puts "Profile folders exist already"
  else
    copy_profile_folders
    puts "Profile folders created successfully"
  end
end

private

def profile_folders_exist?
  profile_folders.each do |f|
     File.directory?(File.join(install_path, f))
  end
end

def profile_folders
   return ["Pictures", "Notes", "Signatures"]
end

def install_path
   Pathname.new(options[:path].to_s)
end
makabde
  • 85
  • 11
  • 2
    The problem in your `profile_folders_exist?` method. You don't return the result of `File.dictionary?` – Yevgeniy Anfilofyev Jan 16 '15 at 14:57
  • You probably want to use the method [`all?`](http://ruby-doc.org/core/Enumerable.html#method-i-all-3F) in place of `each` inside `profile_folders_exist?`. – toro2k Jan 16 '15 at 15:06
  • Thanks @YergeniyAnfilofyev adding `return` in front of `File.directory?` solved the issues. Though I am not sure how to use the method `all?` here. Is there a way to writing this in a more proper way? – makabde Jan 16 '15 at 15:10
  • Are you trying to create empty directories or copy the content of an existing one? – daremkd Jan 16 '15 at 15:24
  • 1
    **NB** Adding `return` in front of `File.directory?` does not solve an issue. It will escape loop immediately. `all?` is the only proper suggestion. Simply change `each` to `all?`. – Aleksei Matiushkin Jan 16 '15 at 15:28
  • @darmkd I am trying to copy the content of an existing one. – makabde Jan 16 '15 at 15:36
  • I changed the `each` with `all` but it will create the folders even if they already exist which is not really what I want. And `return` was fixing the issue actually. – makabde Jan 16 '15 at 15:40
  • possible duplicate of [How to create directories recursively in ruby?](http://stackoverflow.com/questions/3686032/how-to-create-directories-recursively-in-ruby) – Greg Burghardt Jan 16 '15 at 15:53

0 Answers0