class 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

作者

Tanaka Akira <akr@fsij.org>