Project

General

Profile

Actions

Feature #10121

closed

Dir.empty?

Added by shevegen (Robert A. Heiler) about 10 years ago. Updated over 8 years ago.

Status:
Closed
Target version:
-
[ruby-core:64291]

Description

On IRC someone asked how to find out if a directory is empty or not, from within ruby.

It seems as if there was no simple way.

I suggest adding Dir.empty?

On IRC, someone else suggested this:

Dir.entries(dir).size == 2

I do not know if there are better ways but possibly that could
be used for a Dir.empty? functionality?


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #12010: Exclude dot and dotdot from Dir#eachClosedmatz (Yukihiro Matsumoto)Actions

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Description updated (diff)
  • Category set to core
  • Status changed from Open to Feedback

What should happen if the dir is not a directory name?
Just returns false, or raises Errno::ENOTDIR?

Actions #3

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

  • Related to Feature #12010: Exclude dot and dotdot from Dir#each added

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

On Windows, root directories of drives don't contain "." nor "..",
and the suggested code (also Dir.entries(dirname).join == "...") doesn't work.
If it is needed in real case, this method should be adopted, I think.

Actions #5

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Status changed from Feedback to Closed

Applied in changeset r53841.


dir.c: Dir.empty?

  • dir.c (rb_dir_s_empty_p): add Dir.empty? method, which tells the
    argument is the name of an empty directory. [Feature #10121]

Updated by shevegen (Robert A. Heiler) over 8 years ago

Woot! Woot!

Thanks to nobu the patch monster and matz!

Updated by lucasbuchala (Lucas Buchala) over 8 years ago

Now that Ruby got a Dir.empty? method, do you think it makes sense to add a File.empty? as an alias for File.zero? ?

(I didn't know if it was worth opening a new issue for this simple suggestion, so I just appended it here, since I see Dir.empty? and File.empty? as related methods, somehow)

Updated by backus (John Backus) over 8 years ago

I commented this on #9969 as well. Given that Dir.empty? is being introduced I think it would be helpful to also introduce either Pathname#empty? (which would check if something is a file or a directory then do the respective empty check) or Pathname#empty_directory? (which would assume the path is referring to a directory).

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0