Skip to content

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

class method

File.zero?

(::path | ::IO file_name) -> bool
File.zero?(object) → true or false
File.empty?(object) → true or false

Returns whether the given object exists and has size zero.

The given object may be the path to a file:

Example
filepath = '/tmp/t.tmp'
File.write(filepath, 'foo') # File has non-zero size.
File.zero?(filepath)        # => false
File.truncate(filepath, 0)  # File has zero size.
File.zero?(filepath)        # => true
File.delete(filepath)       # Clean up.

The given object may be the path to a directory:

Example
dirpath = '/tmp/foo'
Dir.mkdir(dirpath)
Dir.new(dirpath).children.size # => 0
# Size is filesystem-dependent; may or may not be zero.
File.size(dirpath)             # => 4096
File.zero?(dirpath)            # => false
filepath = '/tmp/foo/t.tmp'    # => "/tmp/foo/t.tmp"
File.write(filepath, 'foo')    # Add a child.
Dir.new(dirpath).children.size # => 1
File.size(dirpath)             # => 4096
File.zero?(dirpath)            # => false
FileUtils.rm_rf(dirpath)       # Clean up.

The given object may be an IO object:

Example
File.zero?($stdin) # => true

The given object may be none of the above:

Example
File.zero?('nosuch') # => false
See also .writable_real? #atime .writable? #birthtime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0