Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > prettyprintライブラリ > PrettyPrintクラス

class PrettyPrint

クラスの継承リスト: PrettyPrint < Object < Kernel

要約

pretty printing アルゴリズムのためのクラスです。 改行の位置を探し、きれいなインデントを施します。

デフォルトでは、このクラスは文字列を扱います。 また、文字1バイトが出力幅の中で1カラムを占めると仮定しています。 しかし、以下のメソッドに対して適切な引数を与えることで、 そうでない場合にも利用できます。

ですので、このクラスは以下のようなことにも応用が可能です。

特異メソッド

定義 説明
format(output = '', maxwidth = 79, newline = "\n", genspace = lambda{|n| ' ' * n}) {|pp| ...} -> object

PrettyPrint オブジェクトを生成し、それを引数としてブロックを実行します。 与えられた output を返します。

new(output = '', maxwidth = 79, newline = "\n") -> PrettyPrint
new(output = '', maxwidth = 79, newline = "\n") {|width| ...} -> PrettyPrint

pretty printing のためのバッファを生成します。 output は出力先です。output は << メソッドを持っていなければなりません。 << メソッドには

のどれかひとつが引数として与えられます。

singleline_format(output = '', maxwidth = 79, newline = "\n", genspace = lambda{|n| ' ' * n}) {|pp| ...} -> object

PrettyPrint オブジェクトを生成し、それを引数としてブロックを実行します。 PrettyPrint.format に似ていますが、改行しません。

インスタンスメソッド

定義 説明
breakable(sep = ' ') -> ()
breakable(sep, width = sep.length) -> ()

「必要ならここで改行出来る」ということを自身に通知します。 もしその位置で改行されなければ、width カラムのテキスト sep が出力の際にそこに挿入されます。

first? -> bool

このメソッドは obsolete です。

flush -> ()

バッファされたデータを出力します。

genspace -> Proc

空白を生成する Proc を返します。

group(indent = 0, open_obj = '', close_obj = '', open_width = open_obj.length, close_width = close_obj.length) {...} -> ()

与えられたブロックを実行します。 ブロック内で自身に追加される文字列やオブジェクトは、1行にまとめて表示しても よい同じグループに属すると仮定されます。

indent -> Integer

現在のインデントの深さを返します。

maxwidth -> Integer

自身の幅を返します。

nest(indent) {...} -> ()

自身の現在のインデントを indent だけ増加させてから、ブロックを実行し、元に戻します。

newline -> String

自身の改行文字を返します。

output -> object

自身の output を返します。

text(obj) -> ()
text(obj, width = obj.length) -> ()

obj を width カラムのテキストとして自身に追加します。

継承したメソッド

== === =~ __id__ __send__ _dump class clone dclone display enum_for eql? equal? extend freeze frozen? hash initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str to_yaml to_yaml_properties to_yaml_style untaint