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#size?

() -> ::Integer?
size? → integer or nil

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.
See also #size #socket? #setuid? #sticky? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0