Similar to IO::Memory, but optimized for building a single string.
You should never have to deal with this class. Instead, use String.build.
Moves the write pointer, and the resulting string bytesize, by the given amount.
Chomps the last byte from the string buffer.
Reads at most slice.size bytes from this IO into slice.
Sets the encoding of this IO.
Returns a nicely readable and concise string representation of this object, typically intended for users.
Writes the contents of slice into this IO.
Writes a single byte into this IO.
Writes the contents of slice, interpreted as a sequence of UTF-8 or ASCII characters, into this IO.
IO
IO
Reference
Reference
Reference
Object
Object
Object
Moves the write pointer, and the resulting string bytesize, by the given amount.
Chomps the last byte from the string buffer. If the byte is '\n' and there's a '\r' before it, it is also removed.
Reads at most slice.size bytes from this IO into slice. Returns the number of bytes read, which is 0 if and only if there is no more data to read (so checking for 0 is the way to detect end of file).
io = IO::Memory.new "hello" slice = Bytes.new(4) io.read(slice) # => 4 slice # => Bytes[104, 101, 108, 108] io.read(slice) # => 1 slice # => Bytes[111, 101, 108, 108] io.read(slice) # => 0
Returns a nicely readable and concise string representation of this object, typically intended for users.
This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.
Also see #inspect.
Writes the contents of slice, interpreted as a sequence of UTF-8 or ASCII characters, into this IO. The contents are transcoded into this IO's current encoding.
bytes = "你".to_slice # => Bytes[228, 189, 160]
io = IO::Memory.new
io.set_encoding("GB2312")
io.write_string(bytes)
io.to_slice # => Bytes[196, 227]
"你".encode("GB2312") # => Bytes[196, 227]
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/String/Builder.html