PHP 8.2.31
Preview: pp.rb Size: 14.16 KB
/proc/thread-self/root/opt/alt/ruby22/lib64/ruby/2.2.0/pp.rb

require 'prettyprint'

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, '')
  end

  private
  # prints arguments in pretty form.
  #
  # pp returns argument(s).
  def pp(*objs) # :nodoc:
    objs.each {|obj|
      PP.pp(obj)
    }
    objs.size <= 1 ? objs.first : objs
  end
  module_function :pp # :nodoc:
end

##
# A pretty-printer for Ruby objects.
#
# All examples assume you have loaded the PP class with:
#   require 'pp'
#
##
# == 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 <tt>obj(s)</tt> to <tt>$></tt> in pretty printed format.
#
# It returns <tt>obj(s)</tt>.
#
##
# == Output Customization
#
# To define a customized pretty printing function for your classes,
# redefine method <code>#pretty_print(pp)</code> in the class.
#
# <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
# object.
#
##
# == Pretty-Print JSON
#
# To pretty-print JSON refer to JSON#pretty_generate.
#
##
# == Author
# Tanaka Akira <akr@fsij.org>

class PP < PrettyPrint
  # Outputs +obj+ to +out+ in pretty printed format of
  # +width+ columns in width.
  #
  # If +out+ is omitted, <code>$></code> is assumed.
  # If +width+ is omitted, 79 is assumed.
  #
  # PP.pp returns +out+.
  def PP.pp(obj, out=$>, width=79)
    q = PP.new(out, width)
    q.guard_inspect_key {q.pp obj}
    q.flush
    #$pp = q
    out << "\n"
  end

  # Outputs +obj+ to +out+ like PP.pp but with no indent and
  # newline.
  #
  # PP.singleline_pp returns +out+.
  def PP.singleline_pp(obj, out=$>)
    q = SingleLine.new(out)
    q.guard_inspect_key {q.pp obj}
    q.flush
    out
  end

  # :stopdoc:
  def PP.mcall(obj, mod, meth, *args, &block)
    mod.instance_method(meth).bind(obj).call(*args, &block)
  end
  # :startdoc:

  @sharing_detection = false
  class << self
    # Returns the sharing detection flag as a boolean value.
    # It is false by default.
    attr_accessor :sharing_detection
  end

  module PPMethods

    # Yields to a block
    # and preserves the previous set of objects being printed.
    def guard_inspect_key
      if Thread.current[:__recursive_key__] == nil
        Thread.current[:__recursive_key__] = {}.taint
      end

      if Thread.current[:__recursive_key__][:inspect] == nil
        Thread.current[:__recursive_key__][:inspect] = {}.taint
      end

      save = Thread.current[:__recursive_key__][:inspect]

      begin
        Thread.current[:__recursive_key__][:inspect] = {}.taint
        yield
      ensure
        Thread.current[:__recursive_key__][:inspect] = save
      end
    end

    # 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(id)
      Thread.current[:__recursive_key__] &&
      Thread.current[:__recursive_key__][:inspect] &&
      Thread.current[:__recursive_key__][:inspect].include?(id)
    end

    # Adds the object_id +id+ to the set of objects being pretty printed, so
    # as to not repeat objects.
    def push_inspect_key(id)
      Thread.current[:__recursive_key__][:inspect][id] = true
    end

    # Removes an object from the set of objects being pretty printed.
    def pop_inspect_key(id)
      Thread.current[:__recursive_key__][:inspect].delete id
    end

    # 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(obj)
      id = obj.object_id

      if check_inspect_key(id)
        group {obj.pretty_print_cycle self}
        return
      end

      begin
        push_inspect_key(id)
        group {obj.pretty_print self}
      ensure
        pop_inspect_key(id) unless PP.sharing_detection
      end
    end

    # A convenience method which is same as follows:
    #
    #   group(1, '#<' + obj.class.name, '>') { ... }
    def object_group(obj, &block) # :yield:
      group(1, '#<' + obj.class.name, '>', &block)
    end

    # A convenience method, like object_group, but also reformats the Object's
    # object_id.
    def object_address_group(obj, &block)
      str = Kernel.instance_method(:to_s).bind(obj).call
      str.chomp!('>')
      group(1, str, '>', &block)
    end

    # A convenience method which is same as follows:
    #
    #   text ','
    #   breakable
    def comma_breakable
      text ','
      breakable
    end

    # 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(list, sep=nil, iter_method=:each) # :yield: element
      sep ||= lambda { comma_breakable }
      first = true
      list.__send__(iter_method) {|*v|
        if first
          first = false
        else
          sep.call
        end
        yield(*v)
      }
    end

    # A present standard failsafe for pretty printing any given Object
    def pp_object(obj)
      object_address_group(obj) {
        seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
          breakable
          v = v.to_s if Symbol === v
          text v
          text '='
          group(1) {
            breakable ''
            pp(obj.instance_eval(v))
          }
        }
      }
    end

    # A pretty print for a Hash
    def pp_hash(obj)
      group(1, '{', '}') {
        seplist(obj, nil, :each_pair) {|k, v|
          group {
            pp k
            text '=>'
            group(1) {
              breakable ''
              pp v
            }
          }
        }
      }
    end
  end

  include PPMethods

  class SingleLine < PrettyPrint::SingleLine # :nodoc:
    include PPMethods
  end

  module ObjectMixin # :nodoc:
    # 1. specific pretty_print
    # 2. specific inspect
    # 3. generic pretty_print

    # A default pretty printing method for general objects.
    # It calls #pretty_print_instance_variables to list instance variables.
    #
    # If +self+ has a customized (redefined) #inspect method,
    # the result of self.inspect is used but it obviously has no
    # line break hints.
    #
    # This module provides predefined #pretty_print methods for some of
    # the most commonly used built-in classes for convenience.
    def pretty_print(q)
      method_method = Object.instance_method(:method).bind(self)
      begin
        inspect_method = method_method.call(:inspect)
      rescue NameError
      end
      if inspect_method && /\(Kernel\)#/ !~ inspect_method.inspect
        q.text self.inspect
      elsif !inspect_method && self.respond_to?(:inspect)
        q.text self.inspect
      else
        q.pp_object(self)
      end
    end

    # A default pretty printing method for general objects that are
    # detected as part of a cycle.
    def pretty_print_cycle(q)
      q.object_address_group(self) {
        q.breakable
        q.text '...'
      }
    end

    # Returns a sorted array of instance variable names.
    #
    # This method should return an array of names of instance variables as symbols or strings as:
    # +[:@a, :@b]+.
    def pretty_print_instance_variables
      instance_variables.sort
    end

    # Is #inspect implementation using #pretty_print.
    # If you implement #pretty_print, it can be used as follows.
    #
    #   alias inspect pretty_print_inspect
    #
    # However, doing this requires that every class that #inspect is called on
    # implement #pretty_print, or a RuntimeError will be raised.
    def pretty_print_inspect
      if /\(PP::ObjectMixin\)#/ =~ Object.instance_method(:method).bind(self).call(:pretty_print).inspect
        raise "pretty_print is not overridden for #{self.class}"
      end
      PP.singleline_pp(self, '')
    end
  end
end

class Array # :nodoc:
  def pretty_print(q) # :nodoc:
    q.group(1, '[', ']') {
      q.seplist(self) {|v|
        q.pp v
      }
    }
  end

  def pretty_print_cycle(q) # :nodoc:
    q.text(empty? ? '[]' : '[...]')
  end
end

class Hash # :nodoc:
  def pretty_print(q) # :nodoc:
    q.pp_hash self
  end

  def pretty_print_cycle(q) # :nodoc:
    q.text(empty? ? '{}' : '{...}')
  end
end

class << ENV # :nodoc:
  def pretty_print(q) # :nodoc:
    h = {}
    ENV.keys.sort.each {|k|
      h[k] = ENV[k]
    }
    q.pp_hash h
  end
end

class Struct # :nodoc:
  def pretty_print(q) # :nodoc:
    q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
      q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
        q.breakable
        q.text member.to_s
        q.text '='
        q.group(1) {
          q.breakable ''
          q.pp self[member]
        }
      }
    }
  end

  def pretty_print_cycle(q) # :nodoc:
    q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
  end
end

class Range # :nodoc:
  def pretty_print(q) # :nodoc:
    q.pp self.begin
    q.breakable ''
    q.text(self.exclude_end? ? '...' : '..')
    q.breakable ''
    q.pp self.end
  end
end

class File < IO # :nodoc:
  class Stat # :nodoc:
    def pretty_print(q) # :nodoc:
      require 'etc.so'
      q.object_group(self) {
        q.breakable
        q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
        q.text "ino="; q.pp self.ino; q.comma_breakable
        q.group {
          m = self.mode
          q.text sprintf("mode=0%o", m)
          q.breakable
          q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
            self.ftype,
            (m & 0400 == 0 ? ?- : ?r),
            (m & 0200 == 0 ? ?- : ?w),
            (m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
                             (m & 04000 == 0 ? ?x : ?s)),
            (m & 0040 == 0 ? ?- : ?r),
            (m & 0020 == 0 ? ?- : ?w),
            (m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
                             (m & 02000 == 0 ? ?x : ?s)),
            (m & 0004 == 0 ? ?- : ?r),
            (m & 0002 == 0 ? ?- : ?w),
            (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
                             (m & 01000 == 0 ? ?x : ?t)))
        }
        q.comma_breakable
        q.text "nlink="; q.pp self.nlink; q.comma_breakable
        q.group {
          q.text "uid="; q.pp self.uid
          begin
            pw = Etc.getpwuid(self.uid)
          rescue ArgumentError
          end
          if pw
            q.breakable; q.text "(#{pw.name})"
          end
        }
        q.comma_breakable
        q.group {
          q.text "gid="; q.pp self.gid
          begin
            gr = Etc.getgrgid(self.gid)
          rescue ArgumentError
          end
          if gr
            q.breakable; q.text "(#{gr.name})"
          end
        }
        q.comma_breakable
        q.group {
          q.text sprintf("rdev=0x%x", self.rdev)
          if self.rdev_major && self.rdev_minor
            q.breakable
            q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
          end
        }
        q.comma_breakable
        q.text "size="; q.pp self.size; q.comma_breakable
        q.text "blksize="; q.pp self.blksize; q.comma_breakable
        q.text "blocks="; q.pp self.blocks; q.comma_breakable
        q.group {
          t = self.atime
          q.text "atime="; q.pp t
          q.breakable; q.text "(#{t.tv_sec})"
        }
        q.comma_breakable
        q.group {
          t = self.mtime
          q.text "mtime="; q.pp t
          q.breakable; q.text "(#{t.tv_sec})"
        }
        q.comma_breakable
        q.group {
          t = self.ctime
          q.text "ctime="; q.pp t
          q.breakable; q.text "(#{t.tv_sec})"
        }
      }
    end
  end
end

class MatchData # :nodoc:
  def pretty_print(q) # :nodoc:
    nc = []
    self.regexp.named_captures.each {|name, indexes|
      indexes.each {|i| nc[i] = name }
    }
    q.object_group(self) {
      q.breakable
      q.seplist(0...self.size, lambda { q.breakable }) {|i|
        if i == 0
          q.pp self[i]
        else
          if nc[i]
            q.text nc[i]
          else
            q.pp i
          end
          q.text ':'
          q.pp self[i]
        end
      }
    }
  end
end

class Object < BasicObject # :nodoc:
  include PP::ObjectMixin
end

[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
  c.class_eval {
    def pretty_print_cycle(q)
      q.text inspect
    end
  }
}

[Numeric, FalseClass, TrueClass, Module].each {|c|
  c.class_eval {
    def pretty_print(q)
      q.text inspect
    end
  }
}

Directory Contents

Dirs: 29 × Files: 75

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
digest DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
drb DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
fiddle DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
io DIR
- drwxr-xr-x 2024-03-03 22:43:16
Edit Download
irb DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
json DIR
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
matrix DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
net DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
openssl DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
optparse DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
psych DIR
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
racc DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rake DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
rbconfig DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
rdoc DIR
- drwxr-xr-x 2024-03-03 22:43:18
Edit Download
rexml DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rinda DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
ripper DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rss DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rubygems DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
shell DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
syslog DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
uri DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
webrick DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
xmlrpc DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
yaml DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
3.46 KB lrw-r--r-- 2014-07-11 08:16:05
Edit Download
2.63 KB lrw-r--r-- 2013-05-19 03:10:21
Edit Download
17.73 KB lrw-r--r-- 2014-09-11 08:09:07
Edit Download
9.77 KB lrw-r--r-- 2014-08-07 06:14:29
Edit Download
9.34 KB lrw-r--r-- 2015-07-03 19:31:05
Edit Download
82.45 KB lrw-r--r-- 2014-11-04 03:21:53
Edit Download
980 B lrw-r--r-- 2023-07-26 14:31:54
Edit Download
29.08 KB lrw-r--r-- 2013-12-14 14:48:36
Edit Download
10.71 KB lrw-r--r-- 2014-07-26 17:11:56
Edit Download
2.79 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
19 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
3.77 KB lrw-r--r-- 2014-08-24 07:16:25
Edit Download
6.42 KB lrw-r--r-- 2014-05-31 19:54:55
Edit Download
26.35 KB lrw-r--r-- 2014-12-12 10:48:57
Edit Download
2.14 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
1.65 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
47.46 KB lrw-r--r-- 2017-03-25 18:24:28
Edit Download
2.48 KB lrw-r--r-- 2014-10-13 07:34:23
Edit Download
8.22 KB lrw-r--r-- 2017-03-25 14:43:16
Edit Download
15.38 KB lrw-r--r-- 2013-05-19 14:50:47
Edit Download
17.06 KB lrw-r--r-- 2015-11-24 15:49:21
Edit Download
20.03 KB lrw-r--r-- 2016-03-28 15:07:06
Edit Download
1.74 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
5.74 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
20.33 KB lrw-r--r-- 2016-10-27 07:47:14
Edit Download
3.84 KB lrw-r--r-- 2014-08-26 12:07:57
Edit Download
53.14 KB lrw-r--r-- 2014-11-27 01:03:46
Edit Download
82.59 KB lrw-r--r-- 2023-07-26 14:29:02
Edit Download
6.93 KB lrw-r--r-- 2012-11-16 16:55:29
Edit Download
2.00 KB lrw-r--r-- 2013-02-20 02:51:51
Edit Download
5.80 KB lrw-r--r-- 2014-08-27 12:21:41
Edit Download
24.58 KB lrw-r--r-- 2014-12-24 09:11:05
Edit Download
20.55 KB lrw-r--r-- 2014-11-04 03:09:28
Edit Download
528 B lrw-r--r-- 2023-07-26 14:31:54
Edit Download
28 B lrw-r--r-- 2014-05-26 03:25:38
Edit Download
52.05 KB lrw-r--r-- 2017-03-25 18:28:27
Edit Download
8.66 KB lrw-r--r-- 2017-03-25 18:35:09
Edit Download
15.58 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
14.16 KB lrw-r--r-- 2014-12-05 19:35:19
Edit Download
15.85 KB lrw-r--r-- 2014-08-22 02:38:59
Edit Download
13.11 KB lrw-r--r-- 2014-12-10 20:38:13
Edit Download
205 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
4.51 KB lrw-r--r-- 2013-05-19 23:04:36
Edit Download
14.55 KB lrw-r--r-- 2014-07-23 19:31:18
Edit Download
14.88 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
2.23 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
4.96 KB lrw-r--r-- 2014-12-07 01:22:37
Edit Download
1.73 KB lrw-r--r-- 2013-03-11 13:47:04
Edit Download
72.06 KB lrw-r--r-- 2015-05-19 17:05:35
Edit Download
2.53 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
2.84 KB lrw-r--r-- 2011-05-11 10:22:16
Edit Download
31.85 KB lrw-r--r-- 2018-02-16 16:27:56
Edit Download
23.54 KB lrw-r--r-- 2014-02-15 01:33:03
Edit Download
9.20 KB lrw-r--r-- 2016-04-22 09:17:57
Edit Download
19.15 KB lrw-r--r-- 2014-08-06 11:28:21
Edit Download
11.30 KB lrw-r--r-- 2014-08-24 07:16:25
Edit Download
5.96 KB lrw-r--r-- 2014-09-05 10:00:46
Edit Download
4.02 KB lrw-r--r-- 2011-05-18 14:09:38
Edit Download
25.60 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
7.25 KB lrw-r--r-- 2013-05-19 03:10:21
Edit Download
11.11 KB lrw-r--r-- 2014-09-21 01:40:21
Edit Download
3.31 KB lrw-r--r-- 2014-08-27 12:10:21
Edit Download
22.25 KB lrw-r--r-- 2015-08-03 19:11:41
Edit Download
3.64 KB lrw-r--r-- 2015-08-10 17:08:37
Edit Download
4.13 KB lrw-r--r-- 2018-03-28 14:34:14
Edit Download
6.40 KB lrw-r--r-- 2013-07-18 13:50:32
Edit Download
14.27 KB lrw-r--r-- 2014-11-26 10:46:50
Edit Download
268 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
8.87 KB lrw-r--r-- 2013-07-05 13:43:25
Edit Download
3.16 KB lrw-r--r-- 2015-01-16 02:06:34
Edit Download
3.07 KB lrw-r--r-- 2014-11-03 00:33:44
Edit Download
2.92 KB lrw-r--r-- 2014-07-26 17:12:11
Edit Download
6.69 KB lrw-r--r-- 2013-10-05 23:39:32
Edit Download
8.49 KB lrw-r--r-- 2013-12-12 03:09:19
Edit Download
1.70 KB lrw-r--r-- 2014-06-26 02:52:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).