W3cubDocs

/Ruby 3

class PP

Parent:
PrettyPrint
Included modules:
PP::PPMethods

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:

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

Usage

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.

Author

Tanaka Akira <[email protected]>

Public Class Methods

pp(obj, out=$>, width=79) Show source
# 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}
  q.flush
  #$pp = q
  out << "\n"
end

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.pp returns out.

sharing_detection() Show source
# File lib/pp.rb, line 100
def sharing_detection
  Ractor.current[:pp_sharing_detection]
end

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

sharing_detection=(b) Show source
# File lib/pp.rb, line 104
def sharing_detection=(b)
  Ractor.current[:pp_sharing_detection] = b
end

Sets the sharing detection flag to b.

singleline_pp(obj, out=$>) Show source
# File lib/pp.rb, line 83
def PP.singleline_pp(obj, out=$>)
  q = SingleLine.new(out)
  q.guard_inspect_key {q.pp obj}
  q.flush
  out
end

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

PP.singleline_pp returns out.

Ruby Core © 1993–2020 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.