Class: PP
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
PrettyPrint
|
|
Instance Chain:
self,
PPMethods ,
PrettyPrint
|
|
Inherits: |
PrettyPrint
|
Defined in: | lib/pp.rb |
Overview
A pretty-printer for Ruby objects.
What PP Does
Standard output by #p
returns this:
#<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 output returns this:
#<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>
Usage
pp(obj) #=> obj
pp obj #=> obj
pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
pp() #=> nil
Output obj(s)
to $>
in pretty printed format.
It returns obj(s)
.
Output Customization
To define a customized pretty printing function for your classes, redefine method #pretty_print(pp)
in the class.
#pretty_print
takes the .pp argument, which is an instance of the PP
class. The method uses #text
, #breakable
, #nest
, #group
and #pp
to print the object.
Pretty-Print JSON
To pretty-print JSON refer to JSON#pretty_generate
.
Author
Tanaka Akira <akr@fsij.org>
Class Attribute Summary
-
.sharing_detection
rw
Returns the sharing detection flag as a boolean value.
-
.sharing_detection=(value)
rw
Returns the sharing detection flag as a boolean value.
Class Method Summary
-
.pp(obj, out = $>, width = width_for(out))
Outputs
obj
toout
in pretty printed format ofwidth
columns in width. -
.singleline_pp(obj, out = $>)
Outputs
obj
toout
like .pp but with no indent and newline. -
.width_for(out)
Returns the usable width for
out
. - .mcall(obj, mod, meth, *args, &block) Internal use only
Instance Method Summary
PPMethods
- Included
#check_inspect_key | Check whether the object_id |
#comma_breakable | A convenience method which is same as follows: |
#guard_inspect_key | Yields to a block and preserves the previous set of objects being printed. |
#object_address_group | A convenience method, like object_group, but also reformats the Object’s object_id. |
#object_group | A convenience method which is same as follows: |
#pop_inspect_key | Removes an object from the set of objects being pretty printed. |
#pp | Adds |
#pp_hash | A pretty print for a |
#pp_object | A present standard failsafe for pretty printing any given |
#push_inspect_key | Adds the object_id |
#seplist | Adds a separated list. |
Class Attribute Details
.sharing_detection (rw)
Returns the sharing detection flag as a boolean value. It is false (nil) by default.
# File 'lib/pp.rb', line 120
def sharing_detection Ractor.current[:pp_sharing_detection] end
.sharing_detection=(value) (rw)
Returns the sharing detection flag as a boolean value. It is false by default.
# File 'lib/pp.rb', line 124
def sharing_detection=(b) Ractor.current[:pp_sharing_detection] = b end
Class Method Details
.mcall(obj, mod, meth, *args, &block)
# File 'lib/pp.rb', line 111
def PP.mcall(obj, mod, meth, *args, &block) mod.instance_method(meth).bind_call(obj, *args, &block) end
.pp(obj, out = $>, width = width_for(out))
Outputs obj
to out
in pretty printed format of width
columns in width.
If out
is omitted, $>
is assumed. If width
is omitted, the width of out
is assumed (see width_for).
pp
returns out
.
# File 'lib/pp.rb', line 91
def PP.pp(obj, out=$>, width=width_for(out)) q = PP.new(out, width) q.guard_inspect_key {q.pp obj} q.flush #$pp = q out << "\n" end
.singleline_pp(obj, out = $>)
Outputs obj
to out
like .pp but with no indent and newline.
singleline_pp
returns out
.
# File 'lib/pp.rb', line 103
def PP.singleline_pp(obj, out=$>) q = SingleLine.new(out) q.guard_inspect_key {q.pp obj} q.flush out end
.width_for(out)
Returns the usable width for out
. As the width of out
:
-
If
out
is assigned to a tty device, its width is used. -
Otherwise, or it could not get the value, the
COLUMN
environment variable is assumed to be set to the width. -
If
COLUMN
is not set to a non-zero number, 80 is assumed.
And finally, returns the above width value - 1.
-
This -1 is for Windows command prompt, which moves the cursor to the next line if it reaches the last column.
# File 'lib/pp.rb', line 74
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