Module: PP::ObjectMixin

Included in:
Object
Defined in:
lib/pp-colour/temp.rb

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.



239
240
241
242
243
244
245
246
247
# File 'lib/pp-colour/temp.rb', line 239

def pretty_print(q)
  if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
    q.text self.inspect.yellow.bold
  elsif /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect && instance_variables.empty?
    q.text self.to_s
  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.



251
252
253
254
255
256
# File 'lib/pp-colour/temp.rb', line 251

def pretty_print_cycle(q)
  q.object_address_group(self) {
    q.breakable
    q.text '...'.green
  }
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.



273
274
275
276
277
278
# File 'lib/pp-colour/temp.rb', line 273

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

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



262
263
264
# File 'lib/pp-colour/temp.rb', line 262

def pretty_print_instance_variables
  instance_variables.sort
end