class String::Builder

A mutable buffer of text, to build a String in pieces without copying at each step. to_s (and freeze) hand the contents over without copying and leave the builder empty; snapshot copies. Indices count characters, as a string's do. A builder is Unshareable: it stays in the task that made it.

b = String::Builder.new
b << "hé" << "llo"
b.size                  # => 5
b.to_s                  # => "héllo"
b.empty?                # => true

<prelude>:2632

Class methods

def self.new -> String::Builder

An empty builder.

<prelude>:2636

Methods

def <<(piece: String) -> Self

Appends piece; returns the builder, so that calls chain.

<prelude>:2638

def push(piece: String) -> Self

Appends piece: the same as <<.

<prelude>:2640

def append(piece: String) -> Self

Appends piece: the same as <<.

<prelude>:2642

def prepend(piece: String) -> Self

Puts piece in front of the contents.

<prelude>:2644

def insert(index: Int, piece: String) -> Self

Inserts piece before character index index (from the end when negative, so that -1 appends).

Raises

IndexError when index is beyond the size.

<prelude>:2651

def delete_at(index: Int) -> String?

Removes the character at character index index (from the end when negative) and returns it; nil, removing nothing, when index is out of range.

b = String::Builder.new
b << "héllo"
b.delete_at(1)          # => "é"
b.delete_at(9)          # => nil
b.to_s                  # => "hllo"

<prelude>:2663

def replace(text: String) -> Self

Replaces the contents with text.

<prelude>:2665

def reverse! -> Self

Reverses the characters in place.

<prelude>:2667

def to_s -> String

The contents, handed over without copying: the builder is left empty and can be filled again.

b = String::Builder.new
b << "ab"
b.to_s                  # => "ab"
b.to_s                  # => ""

<prelude>:2677

def snapshot -> String

A copy of the contents; the builder keeps them.

b = String::Builder.new
b << "ab"
b.snapshot              # => "ab"
b << "c"
b.to_s                  # => "abc"

<prelude>:2687

def freeze -> String

The contents, handed over as to_s does: the builder is left empty.

<prelude>:2689

def size -> Int

The number of characters.

<prelude>:2691

def bytesize -> Int

The number of bytes of the UTF-8 contents.

<prelude>:2693

def empty? -> Bool

Whether the builder holds no character.

<prelude>:2695

def clear -> Self

Removes the contents.

<prelude>:2697