Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Dir.empty?

(::path path_name) -> bool
Dir.empty?(dirpath) → true or false

Returns whether dirpath specifies an empty directory:

Example
dirpath = '/tmp/foo'
Dir.mkdir(dirpath)
Dir.empty?(dirpath)            # => true
Dir.empty?('/example')         # => false
Dir.empty?('/example/main.rb') # => false

Raises an exception if dirpath does not specify a directory or file in the underlying file system.

See also .each_child .entries .delete .exist? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0