January 2024
Intermediate to advanced
718 pages
20h 15m
English
PP uses the PrettyPrint library to format the results of inspecting Ruby objects. In addition to the methods in the class, it defines a global function, pp, which works like the existing p method but formats its output.
PP has a default layout for all Ruby objects. But you can override the way it handles a class by defining the method pretty_print, which takes a PP object as a parameter. It should use that PP object’s methods (text, breakable, nest, group, and pp) to format its output:
| | require 'pp' |
| | |
| | Customer = Struct.new(:first_name, :last_name, :dob, :country) |
| | cust = Customer.new("Walter", "Wall", "12/25/1960", "Niue") |
| | |
| | puts "Regular print" |
| | p cust |
| | |
| | puts "\nPretty print" ... |
Read now
Unlock full access