diff options
Diffstat (limited to 'lib/pp.rb')
-rw-r--r-- | lib/pp.rb | 51 |
1 files changed, 39 insertions, 12 deletions
@@ -46,6 +46,7 @@ require 'prettyprint' # # To define a customized pretty printing function for your classes, # redefine method <code>#pretty_print(pp)</code> in the class. +# Note that <code>require 'pp'</code> is needed before redefining <code>#pretty_print(pp)</code>. # # <code>#pretty_print</code> takes the +pp+ argument, which is an instance of the PP class. # The method uses #text, #breakable, #nest, #group and #pp to print the @@ -61,6 +62,9 @@ require 'prettyprint' # Tanaka Akira <akr@fsij.org> class PP < PrettyPrint + + VERSION = "0.5.0" + # Returns the usable width for +out+. # As the width of +out+: # 1. If +out+ is assigned to a tty device, its width is used. @@ -89,7 +93,7 @@ class PP < PrettyPrint # # PP.pp returns +out+. def PP.pp(obj, out=$>, width=width_for(out)) - q = PP.new(out, width) + q = new(out, width) q.guard_inspect_key {q.pp obj} q.flush #$pp = q @@ -282,16 +286,21 @@ class PP < PrettyPrint group(1, '{', '}') { seplist(obj, nil, :each_pair) {|k, v| group { - pp k - text '=>' - group(1) { - breakable '' - pp v - } + pp_hash_pair k, v } } } end + + # A pretty print for a pair of Hash + def pp_hash_pair(k, v) + pp k + text '=>' + group(1) { + breakable '' + pp v + } + end end include PPMethods @@ -416,9 +425,31 @@ class Struct # :nodoc: end end +class Data # :nodoc: + def pretty_print(q) # :nodoc: + class_name = PP.mcall(self, Kernel, :class).name + class_name = " #{class_name}" if class_name + q.group(1, "#<data#{class_name}", '>') { + q.seplist(PP.mcall(self, Kernel, :class).members, lambda { q.text "," }) {|member| + q.breakable + q.text member.to_s + q.text '=' + q.group(1) { + q.breakable '' + q.pp public_send(member) + } + } + } + end + + def pretty_print_cycle(q) # :nodoc: + q.text sprintf("#<data %s:...>", PP.mcall(self, Kernel, :class).name) + end +end if defined?(Data.define) + class Range # :nodoc: def pretty_print(q) # :nodoc: - q.pp self.begin + q.pp self.begin if self.begin q.breakable '' q.text(self.exclude_end? ? '...' : '..') q.breakable '' @@ -609,10 +640,6 @@ end 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 PP.pp(self, ''.dup) |