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#truncate

(::Integer length) -> 0
truncate(size) → 0

Adjusts the size of file at the path in self to the given size:

Example
pn = Pathname('/tmp/t.tmp')
pn.write('0123456789')
pn.read   # => "0123456789"
pn.truncate(5)
pn.read   # => "01234"

Pads with null characters if necessary:

Example
pn.truncate(10)
pn.read   # => "01234\u0000\u0000\u0000\u0000\u0000"
pn.delete # Clean up.
See also #to_s #unlink #to_path #utime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0