Project

General

Profile

Feature #10121

Dir.empty?

Added by Robert A. Heiler over 1 year ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
[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

Related to Ruby trunk - Feature #12010: Exclude dot and dotdot from Dir#each Assigned

Associated revisions

Revision 53841
Added by Nobuyoshi Nakada 3 months ago

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]

History

#1 [ruby-core:64910] Updated by Nobuyoshi Nakada over 1 year ago

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

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

#3 Updated by Nobuyoshi Nakada 3 months ago

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

#4 [ruby-core:73010] Updated by Nobuyoshi Nakada 3 months ago

  • Assignee set to 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.

#5 Updated by Nobuyoshi Nakada 3 months 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]

#6 [ruby-core:73851] Updated by Robert A. Heiler 3 months ago

Woot! Woot!

Thanks to nobu the patch monster and matz!

#7 [ruby-core:73863] Updated by Lucas Buchala 3 months 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)

Also available in: Atom PDF