Maintenance of Ruby 2.0.0 will end on February 24, 2016. Read more

In Files

  • pp.rb



Included Modules


Pretty-printer for Ruby objects.

Which seems better?

non-pretty-printed output by p is:

#<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 by pp is:

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

I like the latter. If you do too, this library is for you.


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 your customized pretty printing function for your classes, 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::PPMethods#pp to print the object.


Tanaka Akira <>



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

Public Class Methods

pp(obj, out=$>, width=79) click to toggle source

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.

               # File pp.rb, line 76
def PP.pp(obj, out=$>, width=79)
  q =, width)
  q.guard_inspect_key {q.pp obj}
  #$pp = q
  out << "\n"
singleline_pp(obj, out=$>) click to toggle source

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

::singleline_pp returns out.

               # File pp.rb, line 88
def PP.singleline_pp(obj, out=$>)
  q =
  q.guard_inspect_key {q.pp obj}

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit

blog comments powered by Disqus