Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File::Stat#size?
() -> ::Integer?
size? → integer or nil
file.c
View on GitHub ↗
static VALUE
rb_stat_s(VALUE obj)
{
rb_off_t size = get_stat(obj)->ST_(size);
if (size == 0) return Qnil;
return OFFT2NUM(size);
}
Returns the size in bytes of the entry in self if the entry exists and has non-zero size, nil otherwise:
Example
path = '/tmp/t.tmp' File.write(path, 'foo') File.size(path) # => 3 stat = File.stat(path) # Take snapshot. stat.size? # => 3 # Non-zero size. File.write(path, '') File.size(path) # => 0 stat.size? # => 3 # Snapshot unchanged. stat = File.stat(path) # Take new snapshot. stat.size? # => nil # Zero size File.delete(path) # Clean up.