Pretty-printer for Ruby objects.

Which seems better?

non-pretty-printed output by p is:

#<PP:0x806486c @stack=[], @nest=[0], @buf=#<PP::Group:0x8064844 @tail=0, @singleline_length=9, @buf=[#<PP::Group:0x80647cc @tail=0, @singleline_length=9, @buf=[#<PP::Text:0x8064768 @text="[">, #<PP::Group:0x80646c8 @tail=1, @singleline_length=1, @buf=[#<PP::Text:0x8064664 @text="1">]>, #<PP::Text:0x80646b4 @text=",">, #<PP::Breakable:0x8064650 @indent=1, @sep=" ">, #<PP::Group:0x8064614 @tail=1, @singleline_length=1, @buf=[#<PP::Text:0x80645b0 @text="2">]>, #<PP::Text:0x8064600 @text=",">, #<PP::Breakable:0x806459c @indent=1, @sep=" ">, #<PP::Group:0x8064560 @tail=1, @singleline_length=1, @buf=[#<PP::Text:0x80644fc @text="3">]>, #<PP::Text:0x806472c @text="]">]>]>>

pretty-printed output by pp is:

#<PP:0x403279c
 @buf=
  #<PP::Group:0x4032666
   @buf=
    [#<PP::Group:0x4032666
      @buf=
       [#<PP::Text:0x40326de @text="[">,
        #<PP::Group:0x4032666
         @buf=[#<PP::Text:0x40326de @text="1">],
         @singleline_length=1,
         @tail=1>,
        #<PP::Text:0x40326de @text=",">,
        #<PP::Breakable:0x40326b6 @indent=1, @sep=" ">,
        #<PP::Group:0x4032666
         @buf=[#<PP::Text:0x40326de @text="2">],
         @singleline_length=1,
         @tail=1>,
        #<PP::Text:0x40326de @text=",">,
        #<PP::Breakable:0x40326b6 @indent=1, @sep=" ">,
        #<PP::Group:0x4032666
         @buf=[#<PP::Text:0x40326de @text="3">],
         @singleline_length=1,
         @tail=1>,
        #<PP::Text:0x40326de @text="]">],
      @singleline_length=9,
      @tail=0>],
   @singleline_length=9,
   @tail=0>,
 @nest=[0],
 @stack=[]>

I like later. If you like it too, this library is for you.

Usage

pp(obj)

output obj to $> in pretty printed format.

Customization

To define your customized pretty printing function for your class, redefine a method pretty_print(pp) in the class. It takes an argument pp which is an instance of the class PP. The method should use PP#text, PP#breakable, PP#nest, PP#group and PP#pp to print the object.

PP

super class

PrettyPrint

class methods

PP.pp(obj[, width[, out]])

output obj to out in pretty printed format with width width.

methods

pp(obj)

add obj to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycled.

Object#pretty_print_cycled is used when obj is already printing, a.k.a object refarence has a cycle.

Object

pretty_print(pp)

is default pretty printing method for general objects.

If self has customized (redefined) inspect method, a result of self.inspect is used and it has no line break hints.

pretty_print_cycled(pp)

is default pretty printing method for general objects.

PrettyPrint

The class implements pretty printing algorithm. It finds line breaks and nice indentations for grouped structure.

class methods

PrettyPrint.new([newline]) [{|width| ...}]

creates a buffer for pretty printing.

newline is used for line breaks and "\n" is used if it is not specified.

The block is used to generate spaces and {|width| ' ' * indent} is used if it is not given.

methods

text(obj[, width])

add obj as a text with a width width.

If width is not specified, obj.length is used.

breakable([sep[, width]])

add a line breaking hint.

When the buffer is formatted, the hint is treated as a text sep with width width or a line break.

If sep is not specified, " " is used. If width is not specified, sep.length is used.

nest(indent) {...}

increases left margin after newline with indent for line breaks added in the block.

group {...}

groups line break hints added in the block.

format(out[, width])

outputs buffered data to out. It tries restrict maximum line length with width but it may overflow.

If width is not specified, 79 is assumed.

out must have a method named << which accepts a first argument obj of PrettyPrint#text, a first argument sep of PrettyPrint#breakable, a first argument newline of PrettyPrint.new, and a result of a given block for PrettyPrint.new.

Bugs

References

Strictly Pretty, Christian Lindig, March 2000, <URL:http://www.gaertner.de/~lindig/papers/strictly-pretty.html>

A prettier printer, Philip Wadler, March 1998, <URL:http://cm.bell-labs.com/cm/cs/who/wadler/topics/recent.html#prettier>