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 类的实例。该方法使用 text、breakable、nest、group 和 pp 来打印对象。
漂亮打印 JSON
要漂亮打印 JSON,请参阅 JSON#pretty_generate。
作者
Tanaka Akira <akr@fsij.org>
Constants
- VERSION
-
版本字符串
Public Class Methods
Source
# File lib/pp.rb, line 96 def PP.pp(obj, out=$>, width=width_for(out)) q = new(out, width) q.guard_inspect_key {q.pp obj} q.flush #$pp = q out << "\n" end
Source
# File lib/pp.rb, line 125 def sharing_detection Ractor.current[:pp_sharing_detection] end
返回共享检测标志,其值为布尔值。默认情况下为 false (nil)。
Source
# File lib/pp.rb, line 129 def sharing_detection=(b) Ractor.current[:pp_sharing_detection] = b end
将共享检测标志设置为 b。
Source
# File lib/pp.rb, line 108 def PP.singleline_pp(obj, out=$>) q = SingleLine.new(out) q.guard_inspect_key {q.pp obj} q.flush out end
将 obj 输出到 out,类似于 PP.pp,但没有缩进和换行。
PP.singleline_pp 返回 out。
Source
# File lib/pp.rb, line 79 def PP.width_for(out) begin require 'io/console' _, width = out.winsize rescue LoadError, NoMethodError, SystemCallError end (width || ENV['COLUMNS']&.to_i&.nonzero? || 80) - 1 end
返回 out 的可用宽度。作为 out 的宽度
-
如果
out被分配给一个 tty 设备,则使用其宽度。 -
否则,或者无法获取该值,则假定
COLUMN环境变量被设置为宽度。 -
如果
COLUMN未设置为非零数字,则假定为 80。
最后,返回上述宽度值 - 1。
-
此 -1 是为了 Windows 命令行提示符,它会在达到最后一列时将光标移到下一行。