Skip to content

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

instance method

Pathname#size?

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

Returns the size in bytes of the entry at the path in self if the entry exists and has non-zero size, nil otherwise:

Example
# Regular file.
pn = Pathname('/tmp/t.tmp')
pn.write('foo')
pn.size? # => 3     # Non-zero size.
pn.write('')
pn.size? # => nil   # Zero size.
pn.delete           # Clean up.
pn.size? # => nil   # Non-existent.
# Directory.
pn = Pathname('/tmp/foo/')
pn.mkdir
pn.size? # => 4096  # Non-zero size.
pn.delete           # Clean up.
pn.size? # => nil   # Non-existent.
See also #size #socket? #setuid? #split also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0