How can I check to see if a directory is empty or not in Ruby? Is there something like:
Dir.exists?("directory")
(I know that that function doesn’t exist.)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ruby now has
Dir.empty?, making this trivially easy:In Rubies prior to 2.4.0 you can just get a list of the entries and see for yourself whether or not it’s empty (accounting for “.” and “..”). See the docs.
Update: the first method above used to use a regex; now it doesn’t (obviously). Comments below mostly apply to the former (regex) version.