Skip to content

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

.build(capacity : Int = 64, &) : String

View source

.new(capacity : Int = 64)

View source

.new(string : String)

View source

Methods

#back(amount : Int)

Moves the write pointer, and the resulting string bytesize, by the given amount.

View source

#buffer

View source

#bytesize : Int32

View source

#capacity : Int32

View source

#chomp!(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.

View source

#empty?

View source

#read(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
View source

#to_s : 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.

View source

#write(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"
View source

#write_byte(byte : UInt8)

Writes a single byte into this IO.

io = IO::Memory.new
io.write_byte 97_u8
io.to_s # => "a"
View source