类 PP

用于 Ruby 对象的漂亮打印器。

PP 的作用

p 的标准输出返回如下

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

漂亮打印的输出返回如下

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

用法

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

以漂亮打印的格式将 obj(s) 输出到 $>

它返回 obj(s)

输出定制

要为您的类定义自定义的漂亮打印函数,请在类中重新定义方法 pretty_print(pp)。请注意,在重新定义 pretty_print(pp) 之前需要 require 'pp'

pretty_print 接受 pp 参数,它是 PP 类的一个实例。该方法使用 textbreakablenestgrouppp 来打印对象。

漂亮打印 JSON

要漂亮打印 JSON,请参考 JSON#pretty_generate

作者

田中 彰 <akr@fsij.org>