Class | PrettyPrint |
In: |
lib/prettyprint.rb
|
Parent: | Object |
This class implements a pretty printing algorithm. It finds line breaks and nice indentations for grouped structure.
By default, the class assumes that primitive elements are strings and each byte in the strings have single column in width. But it can be used for other situations by giving suitable arguments for some methods:
There are several candidate uses:
Christian Lindig, Strictly Pretty, March 2000, www.st.cs.uni-sb.de/~lindig/papers/#pretty
Philip Wadler, A prettier printer, March 1998, homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
Tanaka Akira <akr@m17n.org>
genspace | [R] | |
group_queue | [R] | |
indent | [R] | |
maxwidth | [R] | |
newline | [R] | |
output | [R] |
This is a convenience method which is same as follows:
begin q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... q.flush output end
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.
This tells "you can break a line here if necessary", and a width\-column text sep is inserted if a line is not broken at the point.
If sep is not specified, " " is used.
If width is not specified, +sep.length+ is used. You will have to specify this when sep is a multibyte character, for example.
Groups line break hints added in the block. The line break hints are all to be used or not.
If indent is specified, the method call is regarded as nested by nest(indent) { … }.
If open_obj is specified, text open_obj, open_width is called before grouping. If close_obj is specified, text close_obj, close_width is called after grouping.
This adds obj as a text of width columns in width.
If width is not specified, obj.length is used.
ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.