# # 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=#> # # Pretty-printed output returns this: # #, # @group_queue= # #], # []]>, # @group_stack= # [#], # @indent=0, # @maxwidth=79, # @newline="\n", # @output=#, # @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 # class PP < PrettyPrint interface _PrettyPrint def pretty_print: (untyped q) -> untyped def pretty_print_cycle: (untyped q) -> untyped def is_a?: (Module) -> bool end interface _LeftShift def <<: (untyped obj) -> self end interface _PPMethodsRequired def text: (String obj, ?Integer width) -> void def breakable: (?String sep, ?Integer width) -> void def group: (?Integer indent, ?String open_obj, ?String close_obj, ?Integer open_width, ?Integer close_width) { () -> untyped } -> void end module PPMethods : _PPMethodsRequired # # Yields to a block and preserves the previous set of objects being printed. # def guard_inspect_key: () { () -> untyped } -> void # # Check whether the object_id `id` is in the current buffer of objects to be # pretty printed. Used to break cycles in chains of objects to be pretty # printed. # def check_inspect_key: (_PrettyPrint id) -> bool # # Adds the object_id `id` to the set of objects being pretty printed, so as to # not repeat objects. # def push_inspect_key: (_PrettyPrint id) -> void # # Removes an object from the set of objects being pretty printed. # def pop_inspect_key: (_PrettyPrint id) -> void # # Adds `obj` to the pretty printing buffer using Object#pretty_print or # Object#pretty_print_cycle. # # Object#pretty_print_cycle is used when `obj` is already printed, a.k.a the # object reference chain has a cycle. # def pp: (_PrettyPrint obj) -> untyped # # A convenience method which is same as follows: # # group(1, '#<' + obj.class.name, '>') { ... } # def object_group: (untyped obj) { () -> untyped } -> Integer # # A convenience method, like object_group, but also reformats the Object's # object_id. # def object_address_group: (untyped obj) { () -> untyped } -> Integer # # A convenience method which is same as follows: # # text ',' # breakable # def comma_breakable: () -> void # # Adds a separated list. The list is separated by comma with breakable space, by # default. # # #seplist iterates the `list` using `iter_method`. It yields each object to the # block given for #seplist. The procedure `separator_proc` is called between # each yields. # # If the iteration is zero times, `separator_proc` is not called at all. # # If `separator_proc` is nil or not given, +lambda { comma_breakable }+ is used. # If `iter_method` is not given, :each is used. # # For example, following 3 code fragments has similar effect. # # q.seplist([1,2,3]) {|v| xxx v } # # q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v } # # xxx 1 # q.comma_breakable # xxx 2 # q.comma_breakable # xxx 3 # def seplist: (untyped list, ?(^() -> void)? sep, ?interned iter_method) { (*untyped, **untyped) -> void } -> void # # A present standard failsafe for pretty printing any given Object # def pp_object: (untyped obj) -> untyped # # A pretty print for a Hash # def pp_hash: (untyped obj) -> untyped end include PPMethods class SingleLine < ::PrettyPrint::SingleLine include PPMethods end module ObjectMixin : BasicObject def pretty_print: (PP q) -> untyped def pretty_print_cycle: (PP q) -> untyped def pretty_print_instance_variables: () -> Array[Symbol] def pretty_print_inspect: () -> untyped end # # Returns the usable width for `out`. As the width of `out`: # 1. If `out` is assigned to a tty device, its width is used. # 2. Otherwise, or it could not get the value, the `COLUMN` environment # variable is assumed to be set to the width. # 3. 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. # def self.width_for: (untyped out) -> Integer # # 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.pp returns `out`. # def self.pp: (_PrettyPrint obj, ?_LeftShift out, ?Integer width) -> untyped # # Outputs `obj` to `out` like PP.pp but with no indent and newline. # # PP.singleline_pp returns `out`. # def self.singleline_pp: (_PrettyPrint obj, ?_LeftShift out) -> untyped def self.mcall: (untyped obj, Module mod, interned meth, *untyped args) ?{ (*untyped, **untyped) -> untyped } -> untyped # # Returns the sharing detection flag as a boolean value. It is false (nil) by # default. # ---- # # Sets the sharing detection flag to b. # attr_accessor self.sharing_detection: bool? end %a{annotate:rdoc:skip} class RubyVM::AbstractSyntaxTree::Node # # def pretty_print_children: (PP q, ?Array[untyped] names) -> void end %a{annotate:rdoc:skip} class Object include PP::ObjectMixin end %a{annotate:rdoc:skip} module Kernel # # Returns a pretty printed object as a string. # # In order to use this method you must first require the PP module: # # require 'pp' # # See the PP module for more information. # def pretty_inspect: () -> String end