Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

PrettyPrint.new

(?untyped output, ?::Integer maxwidth, ?::String newline, ?^(::Integer) -> ::Integer genspace) -> ::PrettyPrint
new(output=''.dup, maxwidth=79, newline="\n", &genspace)

Creates a buffer for pretty printing.

output is an output target. If it is not specified, ” is assumed. It should have a << method which accepts the first argument obj of PrettyPrint#text, the first argument sep of PrettyPrint#breakable, the first argument newline of PrettyPrint.new, and the result of a given block for PrettyPrint.new.

maxwidth specifies maximum line length. If it is not specified, 79 is assumed. However actual outputs may overflow maxwidth if long non-breakable texts are provided.

newline is used for line breaks. “\n” is used if it is not specified.

The block is used to generate spaces. {|width| ‘ ’ * width} is used if it is not given.

See also .format .singleline_format #breakable also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0