Class: PP

Relationships & Source Files
Namespace Children
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, PrettyPrint
Instance Chain:
self, PPMethods, PrettyPrint
Inherits: PrettyPrint
Defined in: lib/pp.rb


A pretty-printer for Ruby objects.

What PP Does

Standard output by #p returns this:

#<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>

Pretty-printed output returns this:

    [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
  [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],


pp(obj)             #=> obj
pp obj              #=> obj
pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
pp()                #=> nil

Output obj(s) to $> in pretty printed format.

It returns obj(s).

Output Customization

To define a customized pretty printing function for your classes, redefine method #pretty_print(pp) in the class.

#pretty_print takes the .pp argument, which is an instance of the PP class. The method uses #text, #breakable, #nest, #group and #pp to print the object.

Pretty-Print JSON

To pretty-print JSON refer to JSON#pretty_generate.


Tanaka Akira <akr@fsij.org>

Class Attribute Summary

Class Method Summary

Instance Method Summary

PPMethods - Included


Check whether the object_id id is in the current buffer of objects to be pretty printed.


A convenience method which is same as follows:


Yields to a block and preserves the previous set of objects being printed.


A convenience method, like object_group, but also reformats the Object's object_id.


A convenience method which is same as follows:


Removes an object from the set of objects being pretty printed.


Adds obj to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycle.


A pretty print for a ::Hash.


A present standard failsafe for pretty printing any given ::Object.


Adds the object_id id to the set of objects being pretty printed, so as to not repeat objects.


Adds a separated list.

Class Attribute Details

.sharing_detection (rw)

Returns the sharing detection flag as a boolean value. It is false by default.

[ GitHub ]

# File 'lib/pp.rb', line 100

attr_accessor :sharing_detection

Class Method Details

.mcall(obj, mod, meth, *args, &block)

This method is for internal use only.
[ GitHub ]

# File 'lib/pp.rb', line 91

def PP.mcall(obj, mod, meth, *args, &block)
  mod.instance_method(meth).bind_call(obj, *args, &block)

.pp(obj, out = $>, width = 79)

Outputs obj to out in pretty printed format of width columns in width.

If out is omitted, $> is assumed. If width is omitted, 79 is assumed.

pp returns out.

[ GitHub ]

# File 'lib/pp.rb', line 71

def PP.pp(obj, out=$>, width=79)
  q = PP.new(out, width)
  q.guard_inspect_key {q.pp obj}
  #$pp = q
  out << "\n"

.singleline_pp(obj, out = $>)

Outputs obj to out like .pp but with no indent and newline.

singleline_pp returns out.

[ GitHub ]

# File 'lib/pp.rb', line 83

def PP.singleline_pp(obj, out=$>)
  q = SingleLine.new(out)
  q.guard_inspect_key {q.pp obj}