Skip to content

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

instance method

File::Stat#zero?

() -> bool
zero? → true or false

Returns whether the entry at the path in self has size zero.

The entry may be a file:

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

The entry may be a directory:

Example
dirpath = '/tmp/foo'
Dir.mkdir(dirpath)
stat = File.stat(dirpath)
# Size is filesystem-dependent; may or may not be zero.
stat.size                              # => 4096
stat.zero?                             # => false
filepath = File.join(dirpath, 't.tmp') # => "/tmp/foo/t.tmp"
File.write(filepath, 'foo')
stat = File.stat(dirpath)
stat.size                              # => 4096
stat.zero?                             # => false
FileUtils.rm_rf(dirpath)               # Clean up.
See also #writable_real? #writable? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0