首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

PP

Parent:PrettyPrintIncluded模块:PP :: PPMethods

一个适合Ruby对象的漂亮打印机。

所有例子都假设你已经加载了PP类:

代码语言:javascript
复制
require 'pp'

PP做什么

标准输出由p返回:

代码语言:javascript
复制
#<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>>

好的打印输出返回这个:

代码语言:javascript
复制
#<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>

用法

代码语言:javascript
复制
pp(obj)             #=> obj
pp obj              #=> obj
pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
pp()                #=> nil

输出obj(s)$>好的打印格式。

It returns obj(s).

输出定制

为您的类定义好的的打印功能,重新定义类中的方法#pretty_print(pp)

#pretty_print采用pp参数,这是PP类的一个实例。该方法使用text,breakable,nest,group和pp来打印对象。

Pretty-Print JSON

要好地打印JSON,请参阅JSON#pretty_generate。

以布尔值形式返回共享检测标志。它默认为false。

Public Class Methods

pp(obj, out=$>, width=79) Show source

输出obj为宽度out很好的width列的格式。

如果out省略,$>则假定。如果width省略,则假定为79。

::pp returns out.

代码语言:javascript
复制
# File lib/pp.rb, line 97
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=$>) Show source

输出objout像:: pp,但没有缩进和换行符。

::singleline_pp returns out.

代码语言:javascript
复制
# File lib/pp.rb, line 109
def PP.singleline_pp(obj, out=$>)
  q = SingleLine.new(out)
  q.guard_inspect_key {q.pp obj}
  q.flush
  out
end

扫码关注腾讯云开发者

领取腾讯云代金券