Class: PP

Inherits:
PrettyPrint show all
Includes:
PPMethods
Defined in:
opal/stdlib/pp.rb

Overview

A pretty-printer for Ruby objects.

== What PP Does

Standard output by #p returns this: #, @group_queue=#], []]>, @buffer=[], @newline="\n", @group_stack=[#], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#IO:0x8114ee4>

Pretty-printed output returns this: #, @group_queue= #], []]>, @group_stack= [#], @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 [email protected]

Direct Known Subclasses

REPLUtils::ColorPrinter

Defined Under Namespace

Modules: ObjectMixin, PPMethods Classes: SingleLine

Class Attribute Summary collapse

Attributes inherited from PrettyPrint

#genspace, #group_queue, #indent, #maxwidth, #newline, #output

Class Method Summary collapse

Methods included from PPMethods

#check_inspect_key, #comma_breakable, #guard_inspect_key, #object_address_group, #object_group, #pop_inspect_key, #pp, #pp_hash, #pp_object, #push_inspect_key, #seplist

Methods inherited from PrettyPrint

#break_outmost_groups, #breakable, #current_group, #fill_breakable, #flush, format, #group, #group_sub, #initialize, #nest, singleline_format, #text

Constructor Details

This class inherits a constructor from PrettyPrint

Class Attribute Details

.sharing_detectionObject

Returns the sharing detection flag as a boolean value. It is false by default.



103
104
105
# File 'opal/stdlib/pp.rb', line 103

def sharing_detection
  @sharing_detection
end

Class Method Details

.mcall(obj, mod, meth, *args, &block) ⇒ Object

:stopdoc:



94
95
96
# File 'opal/stdlib/pp.rb', line 94

def PP.mcall(obj, mod, meth, *args, &block)
  mod.instance_method(meth).bind_call(obj, *args, &block)
end

.pp(obj, out = $stdout, width = 79) ⇒ Object

Outputs +obj+ to +out+ in pretty printed format of +width+ columns in width.

If +out+ is omitted, $> is assumed. If +width+ is omitted, 79 is assumed.

PP.pp returns +out+.



74
75
76
77
78
79
80
# File 'opal/stdlib/pp.rb', line 74

def PP.pp(obj, out=$stdout, width=79)  # Opal: replace $> with $stdout
  q = PP.new(out, width)
  q.guard_inspect_key {q.pp obj}
  q.flush
  #$pp = q
  out << "\n"
end

.singleline_pp(obj, out = $stdout) ⇒ Object

Outputs +obj+ to +out+ like PP.pp but with no indent and newline.

PP.singleline_pp returns +out+.



86
87
88
89
90
91
# File 'opal/stdlib/pp.rb', line 86

def PP.singleline_pp(obj, out=$stdout)  # Opal: replace $> with $stdout
  q = SingleLine.new(out)
  q.guard_inspect_key {q.pp obj}
  q.flush
  out
end