Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > library pp
library pp
要約
オブジェクトなどを見やすく出力するためのライブラリです。
このライブラリを require すると Kernel.#pp が定義されます。 Kernel.#p のかわりに Kernel.#pp を使うことにより、 適切にインデントと改行された分かりやすい出力を得ることが出来ます。pp ライブラリは、 ユーザがあたらしく定義したクラスに対しても見やすい表示を するように作られていますので、Kernel.#pp を使う上で余計な作業をする 必要はありません。
どちらが読みやすいでしょうか?
p による pretty-print されてない出力:
#<PP:0x81a0d10 @stack=[], @genspace=#<Proc:0x81a0cc0>, @nest=[0], @newline="\n", @buf=#<PrettyPrint::Group:0x81a0c98 @group=0, @tail=0, @buf=[#<PrettyPrint::Gro up:0x81a0ba8 @group=1, @tail=0, @buf=[#<PrettyPrint::Text:0x81a0b30 @tail=2, @wi dth=1, @text="[">, #<PrettyPrint::Group:0x81a0a68 @group=2, @tail=1, @buf=[#<Pre ttyPrint::Text:0x81a09f0 @tail=1, @width=1, @text="1">], @singleline_width=1>, # <PrettyPrint::Text:0x81a0a7c @tail=0, @width=1, @text=",">, #<PrettyPrint::Break able:0x81a0a2c @group=2, @gensace=#<Proc:0x81a0cc0>, @newline="\n", @indent=1, @ tail=2, @sep=" ", @width=1>, #<PrettyPrint::Group:0x81a09c8 @group=2, @tail=1, @ buf=[#<PrettyPrint::Text:0x81a0950 @tail=1, @width=1, @text="2">], @singleline_w idth=1>, #<PrettyPrint::Text:0x81a0af4 @tail=0, @width=1, @text="]">], @singleli ne_width=6>], @singleline_width=6>, @sharing_detection=false>
pp による pretty-print された出力:
#<PP:0x40d0688
@buf=
#<PrettyPrint::Group:0x40d064c
@buf=
[#<PrettyPrint::Group:0x40d05d4
@buf=
[#<PrettyPrint::Text:0x40d0598 @tail=2, @text="[", @width=1>,
#<PrettyPrint::Group:0x40d0534
@buf=[#<PrettyPrint::Text:0x40d04f8 @tail=1, @text="1", @width=1>],
@group=2,
@singleline_width=1,
@tail=1>,
#<PrettyPrint::Text:0x40d053e @tail=0, @text=",", @width=1>,
#<PrettyPrint::Breakable:0x40d0516
@genspace=#<Proc:0x40d0656>,
@group=2,
@indent=1,
@newline="\n",
@sep=" ",
@tail=2,
@width=1>,
#<PrettyPrint::Group:0x40d04e4
@buf=[#<PrettyPrint::Text:0x40d04a8 @tail=1, @text="2", @width=1>],
@group=2,
@singleline_width=1,
@tail=1>,
#<PrettyPrint::Text:0x40d057a @tail=0, @text="]", @width=1>],
@group=1,
@singleline_width=6,
@tail=0>],
@group=0,
@singleline_width=6,
@tail=0>,
@genspace=#<Proc:0x40d0656>,
@nest=[0],
@newline="\n",
@sharing_detection=false,
@stack=[]>
出力のカスタマイズ
あるクラスの pp の出力をカスタマイズしたい場合は、 そのクラスで pretty_print メソッドと pretty_print_cycle メソッドを再定義します。 このメソッドは PP オブジェクトを引数として pp 実行時に呼ばれます。 ユーザは表示したい内容を表すツリーを、 引数として与えられた PP オブジェクトを使って以下のように作成します。
- PrettyPrint#group を使って子ノードをつくります。同時に子ノードのインデントの深さも決めます。
- PrettyPrint#breakable を使って改行しても良い場所を指定します。
- PP#pp を使って出力したいインスタンス変数などを出力します。
- PrettyPrint#text を使って、出力が見やすくなるように「,」などの修飾文字を適宜挿入します。
PP は PrettyPrint のサブクラスですので、上で PrettyPrint のメソッドとされているものは PP のメソッドでもあります。
以下は Hash の pretty printing のカスタマイズの例です。
require 'pp'
class Hash
def pretty_print(q)
q.group(2, "<hash>") do
q.breakable
first = true
self.each{|k, v|
unless first
q.text(',')
q.breakable
end
q.pp k
q.text ' => '
q.group(1) do
q.breakable ''
if v.is_a?(String) and v.size > 10
q.pp(v[0..9] + '...')
else
q.pp v
end
end
first = false
}
end
q.breakable
q.text "</hash>"
end
def pretty_print_cycle(q)
q.text(empty? ? '{}' : '{...}')
end
end
h = {:a => 'a'*5, :b => 'b'*10, :c => 'c'*20, :d => 'd'*30}
pp h
#=>
<hash>
:d => "dddddddddd...",
:a => "aaaaa",
:b => "bbbbbbbbbb",
:c => "cccccccccc..."
</hash>
クラスとモジュール
| class PP | オブジェクトなどを見やすく出力するためのクラスです。 |
追加・再定義されるメソッド
Kernel.#pp(*obj) -> nil[added by pp]-
指定されたオブジェクト obj を標準出力に見やすい形式(プリティプリント)で出力します。 obj それぞれを引数として PP.pp を呼びことと同等です。
- [PARAM] obj:
- 表示したいオブジェクトを指定します。
例:
require 'pp' b = [1, 2, 3] * 4 a = [b, b] a << a pp a #=> [[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [...]][SEE_ALSO] PP.pp
Object#pretty_inspect -> String[added by pp]-
self を pp で表示したときの結果を文字列として返します。
Object#pretty_print(pp) -> ()[added by pp]-
PP.pp や Kernel.#pp がオブジェクトの内容を出力するときに 呼ばれるメソッドです。PP オブジェクト pp を引数として呼ばれます。
あるクラスの pp の出力をカスタマイズしたい場合は、このメソッドを再定義します。 そのとき pretty_print メソッドは指定された pp に対して表示したい自身の内容を追加して いかなければいけません。いくつかの組み込みクラスについて、 pp ライブラリはあらかじめ pretty_print メソッドを定義しています。
- [PARAM] pp:
- PP オブジェクトです。
例:
class Array def pretty_print(q) q.group(1, '[', ']') { q.seplist(self) {|v| q.pp v } } end end[SEE_ALSO] Object#pretty_print_cycle, Object#inspect, PrettyPrint#text, PrettyPrint#group, PrettyPrint#breakable
Object#pretty_print_cycle(pp) -> ()[added by pp]-
プリティプリント時にオブジェクトの循環参照が検出された場合、 Object#pretty_print の代わりに呼ばれるメソッドです。
あるクラスの pp の出力をカスタマイズしたい場合は、 このメソッドも再定義する必要があります。
- [PARAM] pp:
- PP オブジェクトです。
例:
class Array def pretty_print_cycle(q) q.text(empty? ? '[]' : '[...]') end end[SEE_ALSO] Object#pretty_print
Object#pretty_print_inspect -> String[added by pp]-
Object#pretty_print を使って Object#inspect と同様に オブジェクトを人間が読める形式に変換した文字列を返します。
出力する全てのオブジェクトに Object#pretty_print が定義されている必要があります。 そうでない場合には RuntimeError が発生します。
- [EXCEPTION] RuntimeError:
- 出力する全てのオブジェクトに Object#pretty_print が定義されて いない場合に発生します。
Object#pretty_print_instance_variables -> [String | Symbol][added by pp]-
プリティプリント時に表示すべき自身のインスタンス変数名の配列をソートして返します。 返されたインスタンス変数はプリティプリント時に表示されます。
pp に表示したくないインスタンス変数がある場合にこのメソッドを再定義します。