Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
FileTest#zero?
(::path | ::io file_name) -> bool
File.zero?(object) → true or false
file.c
View on GitHub ↗
static VALUE
rb_file_zero_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
return RBOOL(st.st_size == 0);
}
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