Class: PP

Inherits:
PrettyPrint show all
Includes:
PPMethods
Defined in:
lib/pp.rb

Overview

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:

#<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>

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

Usage

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#pp to print the object.

Author

Tanaka Akira <[email protected]>

Defined Under Namespace

Modules: ObjectMixin, PPMethods Classes: SingleLine

Constant Summary

Constants included from PPMethods

PPMethods::PointerMask

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from PPMethods

#check_inspect_key, #comma_breakable, #guard_inspect_key, #object_address_group, #object_group, #pop_inspect_key, #pp, #pp_hash, #pp_object, #push_inspect_key, #seplist

Class Attribute Details

.sharing_detectionObject

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



105
106
107
# File 'lib/pp.rb', line 105

def sharing_detection
  @sharing_detection
end

Class Method Details

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

:stopdoc:



96
97
98
# File 'lib/pp.rb', line 96

def PP.mcall(obj, mod, meth, *args, &block)
  mod.instance_method(meth).bind(obj).call(*args, &block)
end

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

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.



76
77
78
79
80
81
82
# File 'lib/pp.rb', line 76

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

.singleline_pp(obj, out = $>) ⇒ Object

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

PP.singleline_pp returns out.



88
89
90
91
92
93
# File 'lib/pp.rb', line 88

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