Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • pp.rb

PP::ObjectMixin

Public Instance Methods

pretty_print(q) click to toggle source

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.

 
               # File pp.rb, line 279
def pretty_print(q)
  method_method = Object.instance_method(:method).bind(self)
  begin
    inspect_method = method_method.call(:inspect)
  rescue NameError
  end
  begin
    to_s_method = method_method.call(:to_s)
  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
  elsif to_s_method && /\(Kernel\)#/ !~ to_s_method.inspect
    q.text self.to_s
  elsif !to_s_method && self.respond_to?(:to_s)
    q.text self.to_s
  else
    q.pp_object(self)
  end
end
            
pretty_print_cycle(q) click to toggle source

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

 
               # File pp.rb, line 304
def pretty_print_cycle(q)
  q.object_address_group(self) {
    q.breakable
    q.text '...'
  }
end
            
pretty_print_inspect() click to toggle source

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.

 
               # File pp.rb, line 326
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_variables() click to toggle source

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

 
               # File pp.rb, line 315
def pretty_print_instance_variables
  instance_variables.sort
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus