Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

File#truncate

file.truncate(integer) → 0

Truncates file to at most integer bytes. The file must be opened for writing. Not available on all platforms.

Example
f = File.new("out", "w")
f.syswrite("1234567890")   #=> 10
f.truncate(5)              #=> 0
f.close()                  #=> nil
File.size("out")           #=> 5
See also .truncate #to_path #size also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0