module PP::PPMethods - Documentation for Ruby 4.0
Description: module PP::PPMethods: Module that defines helper methods for pretty_print.
Keywords: ruby,module,PP::PPMethods

🔍

No recent searches

# module PP::PPMethods

Module that defines helper methods for pretty_print.

### Public Instance Methods

check_inspect_key (id)
Source

    # File lib/pp.rb, line 161
    def check_inspect_key(id)
      recursive_state = Thread.current[:__recursive_key__] or return false
      recursive_state[:inspect]&.include?(id)
    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.

comma_breakable ()
Source

    # File lib/pp.rb, line 238
    def comma_breakable
      text ','
      breakable
    end

A convenience method which is same as follows:

    text ','
    breakable

guard_inspect_key () { || ... }
Source

    # File lib/pp.rb, line 147
    def guard_inspect_key
      recursive_state = Thread.current[:__recursive_key__] ||= {}.compare_by_identity
      save = recursive_state[:inspect] ||= {}.compare_by_identity
      begin
        recursive_state[:inspect] = {}.compare_by_identity
        yield
      ensure
        recursive_state[:inspect] = save
      end
    end

Yields to a block and preserves the previous set of objects being printed.

object_address_group (obj, &block)
Source

    # File lib/pp.rb, line 228
    def object_address_group(obj, &block)
      str = Kernel.instance_method(:to_s).bind_call(obj)
      str.chomp!('>')
      group(1, str, '>', &block)
    end

A convenience method, like `object_group`, but also reformats the Object’s object_id.

object_group (obj) { || ... }
Source

    # File lib/pp.rb, line 222
    def object_group(obj, &block) # :yield:
      group(1, '#<' + obj.class.name, '>', &block)
    end

A convenience method which is same as follows:

    group(1, '#<' + obj.class.name, '>') { ... }

pop_inspect_key (id)
Source

    # File lib/pp.rb, line 173
    def pop_inspect_key(id)
      Thread.current[:__recursive_key__][:inspect].delete id
    end

Removes an object from the set of objects being pretty printed.

pp (obj)
Source

    # File lib/pp.rb, line 200
    def pp(obj)
      # If obj is a Delegator then use the object being delegated to for cycle
      # detection
      obj = obj.__getobj__ if defined?(::Delegator) and ::Delegator === obj

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

      guard_inspect(obj) do
        group do
          obj.pretty_print self
        rescue NoMethodError
          text Kernel.instance_method(:inspect).bind_call(obj)
        end
      end
    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.

pp_hash (obj)
Source

    # File lib/pp.rb, line 302
    def pp_hash(obj)
      group(1, '{', '}') {
        seplist(obj, nil, :each_pair) {|k, v|
          group {
            pp_hash_pair k, v
          }
        }
      }
    end

A pretty print for a `Hash`

pp_hash_pair (k, v)
Source

    # File lib/pp.rb, line 314
    def pp_hash_pair(k, v)
      if Symbol === k
        if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z])
          k = k.to_s.inspect
        end
        text "#{k}:"
      else
        pp k
        text ' '
        text '=>'
      end
      group(1) {
        breakable
        pp v
      }
    end

A pretty print for a pair of `Hash`

pp_object (obj)
Source

    # File lib/pp.rb, line 286
    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 present standard failsafe for pretty printing any given `Object`

push_inspect_key (id)
Source

    # File lib/pp.rb, line 168
    def push_inspect_key(id)
      Thread.current[:__recursive_key__][:inspect][id] = true
    end

Adds the object_id `id` to the set of objects being pretty printed, so as to not repeat objects.

seplist (list, sep=nil, iter_method=:each) { |element| ... }
Source

    # File lib/pp.rb, line 267
    def seplist(list, sep=nil, iter_method=:each) # :yield: element
      sep ||= lambda { comma_breakable }
      first = true
      kwsplat = EMPTY_KWHASH
      list.__send__(iter_method) {|*v|
        if first
          first = false
        else
          sep.call
        end
        kwsplat ? yield(*v, **kwsplat) : yield(*v)
      }
    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