class String::Builder
inherits IO
¶
Similar to IO::Memory
, but optimized for building a single string.
You should never have to deal with this class. Instead, use String.build
.
Class methods¶
Methods¶
#back(amount : Int)
¶
(amount : Int)
Moves the write pointer, and the resulting string bytesize, by the given amount.
#chomp!(byte : UInt8)
¶
(byte : UInt8)
Chomps the last byte from the string buffer.
If the byte is '\n'
and there's a '\r'
before it, it is also removed.
#read(slice : Bytes)
¶
(slice : Bytes)
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
#to_s : String
¶
: String
Returns a string representation of this object.
Descendants must usually not override this method. Instead,
they must override to_s(io)
, which must append to the given
IO object.
#write(slice : Bytes) : Nil
¶
(slice : Bytes) : Nil
Writes the contents of slice into this IO
.
io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"
#write_byte(byte : UInt8)
¶
(byte : UInt8)
Writes a single byte into this IO
.
io = IO::Memory.new
io.write_byte 97_u8
io.to_s # => "a"