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
pathname_builtin.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 1965
def truncate(length) File.truncate(@path, length) end
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.