Skip to content

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

instance method

Pathname#empty?

() -> bool
empty? → true or false

Returns whether the entry represented by self exists and is empty:

Example
dir_pn = Pathname('example_dir')
dir_pn.empty?  # => false  # Dir does not exist.
dir_pn.mkdir
dir_pn.empty?  # => true   # Dir exists and is empty.

file_pn = Pathname('example_dir/example.txt')
file_pn.empty? # => false  # File does not exist.
file_pn.write('')
file_pn.empty? # => true   # File exists and is empty.
dir_pn.empty?  # => false  # Dir exists and is not empty.
file_pn.write('foo')
file_pn.empty? # => false  # File exists and is not empty.

file_pn.delete
dir_pn.delete
See also #each_line #entries #each_filename #eql? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0