Project

General

Profile

Feature #16249

Dir#empty? and File#empty?

Added by zverok (Victor Shepelev) about 1 month ago. Updated 24 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:95306]

Description

I don't think it is a super-valuable addition (as nobody complained about it, or I can't find similar proposals), but seems logical to me:

# Existed since 2.4
Dir.empty?('dirname') #=> true/false
File.empty?('filename') # => true/false

# Proposed and implemented:
Dir.open('dirname').empty?
File.open('filename').empty?

Files

file_dir_i_empty.patch (3.27 KB) file_dir_i_empty.patch zverok (Victor Shepelev), 10/12/2019 09:47 AM

History

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

    return rb_dir_s_empty_p(rb_cDir, rb_str_dup(dirp->path));

This checks a directory named the current Dir object opened, instead of the opened directory itself.

Updated by shevegen (Robert A. Heiler) about 1 month ago

Personally I think I have wanted/needed this in the past. It has not been
that important to me to suggest it myself, though, but +1 on the name/API.

(as nobody complained about it, or I can't find similar proposals), but
seems logical to me:

I think not every thought process alone makes it into a suggestion. :)

A good example is require 'pp'; I was thinking that it would be convenient
before it was suggested to change it, but I never made a proposal myself.
Inertia sometimes wins ... but rubyists may often "think alike", or
in a somewhat similar way, to some extent.

I guess it (whether the API has not existed before) may have to do with what
is/should be possible through FileUtils. So perhaps the opinion is or was
that the file-related activities, directory-related activities etc... should
all go through FileUtils instead.

We already do have a few file-related activities outside of FileUtils,
though, such as File.delete(). I use the latter a lot, more often than
tapping into FileUtils or the .rm_rf variant (if the task is to delete
the file that is); oddly enough, one minor reason is so that I can avoid
having to do require 'fileutils', a bit similar as to how I like how
require 'pp' became optional - but anyway, I digress, so +1 to the
suggestion in itself.

(I have no particular opinion on the implementation either way; my comment
here is really meant to the Dir.empty?() and File.empty?() API/suggestion
alone.)

Updated by zverok (Victor Shepelev) 24 days ago

nobu (Nobuyoshi Nakada)

This checks a directory named the current Dir object opened, instead of the opened directory itself.

I am not sure I understand (probably it is my lack of in-depth filesystem work understanding), for what I can tell, we have a directory object with some path, and check if directory with this path is empty?..

Also available in: Atom PDF