Module: PP::ObjectMixin

Included in:
Object
Defined in:
lib/pp.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#pretty_print(q) ⇒ Object

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.



351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/pp.rb', line 351

def pretty_print(q)
  umethod_method = Object.instance_method(:method)
  begin
    inspect_method = umethod_method.bind_call(self, :inspect)
  rescue NameError
  end
  if inspect_method && inspect_method.owner != Kernel
    q.text self.inspect
  elsif !inspect_method && self.respond_to?(:inspect)
    q.text self.inspect
  else
    q.pp_object(self)
  end
end

#pretty_print_cycle(q) ⇒ Object

A default pretty printing method for general objects that are detected as part of a cycle.



368
369
370
371
372
373
# File 'lib/pp.rb', line 368

def pretty_print_cycle(q)
  q.object_address_group(self) {
    q.breakable
    q.text '...'
  }
end

#pretty_print_inspectObject

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.



390
391
392
393
394
395
# File 'lib/pp.rb', line 390

def pretty_print_inspect
  if Object.instance_method(:method).bind_call(self, :pretty_print).owner == PP::ObjectMixin
    raise "pretty_print is not overridden for #{self.class}"
  end
  PP.singleline_pp(self, ''.dup)
end

#pretty_print_instance_variablesObject

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].



379
380
381
# File 'lib/pp.rb', line 379

def pretty_print_instance_variables
  instance_variables.sort
end