Reason #174 • June 23rd, 2026

Color printing with IRB::ColorPrinter.pp

Over the last two days we have talked about using Kernel#p and Kernel#pp for print debugging.

However, we're not done with print debugging just yet!

You may have noticed while playing around with these methods in IRB that the output from pp is monochrome, while IRB's own evaluation output is colorized. If IRB supposedly uses pp under the hood, where is the colorization coming from?

Turns out IRB has its own pretty printer, IRB::ColorPrinter, which is a subclass of PP that adds colorization to the output. It is used by IRB to print expression results, but it can also be used directly in our own code:

Ruby
require "irb/color_printer"

list = 10.times.map do |i|
  { string: "Hello, world!", symbol: :hello, integer: i }
end

puts "Kernel.pp"
puts

pp list
# => [{ string: "Hello, world!", symbol: :hello, integer: 0 }, ...]

puts
puts "IRB::ColorPrinter.pp"
puts

IRB::ColorPrinter.pp list
# => #<IO:<STDOUT>>
    

Result:

Yes, that sure is the formatting we know and love from IRB!

However, IRB::ColorPrinter.pp is not a drop-in replacement for Kernel#pp, because it does not treat multiple arguments the same and does not return the object(s) printed. This means that our pp monkey patch (yes, this is what we're doing!) requires a bit more work than just delegating the method call:

Ruby
module Kernel
  def pp(*args)
    return nil if args.empty?
    args.each(&IRB::ColorPrinter.method(:pp))
    args.length == 1 ? args.first : args
  end

  module_function :pp
end
    

And with that, all our wildest print debugging dreams have come true!

History

IRB colorization predates IRB::ColorPrinter. Ruby 2.7, released on Christmas 2019, shipped IRB 1.2.1 with syntax highlighting and the --colorize / --nocolorize options.

IRB::ColorPrinter was added to IRB at the end of 2020. It first shipped with Ruby 3.0.1 through IRB 1.3.5.