當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby PP類用法及代碼示例

本文簡要介紹ruby語言中 PP類 的用法。

Ruby 對象的pretty-printer。

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

Pretty-printed 輸出返回:

#<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 參數,它是 PP 類的一個實例。該方法使用 text breakable nest group pp 來打印對象。

Pretty-Print JSON

至 pretty-print JSON 請參閱 JSON#pretty_generate

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 PP類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。