Skip to content

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

class method

File.truncate

(::path file_name, ::int length) -> 0
File.truncate(filepath, size) → 0

Adjusts the size of file filepath to the given size:

Example
filepath = '/tmp/t.tmp'
File.write(filepath, '0123456789')
File.read(filepath)   # => "0123456789"
File.truncate(filepath, 5)
File.read(filepath)   # => "01234"

Pads with null characters if necessary:

Example
File.truncate(filepath, 10)
File.read(filepath)   # => "01234\u0000\u0000\u0000\u0000\u0000"
File.delete(filepath) # Clean up.
See also #truncate .symlink? .umask .symlink also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0