Formatters

The following formatter implementation are provided by default.

They are implemented by adding the report_print method onto the specified class, and so will be inherited as expected and can be overwritten as needed.

Simple Values

A number of simple value types are configured to produce colorized versions of their inspect output.

rp :symbols
rp "strings"

# All Numeric types
rp 1
rp 0.5
rp Float::INFINITY
rp 1i * 1i
rp Rational(2, 3)

# Constants
rp true
rp false
rp nil

Output:

:symbols
"strings"
1
0.5
Infinity
(-1+0i)
(2/3)
true
false
nil

Object

class A
  def initialize
    @name = self.class.name
  end
end

class B < A
  def initialize
    super
    @field = A.new
  end
end

rp B.new

Output:

B 0x2b0
  @name = "B"
  @field = A 0x2b8
    @name = "A"
  end
end

Module

rp Class

Output:

Class

Array

rp [1, "two", [[]]]

Output:

[
  1,
  "two",
  [
    []
  ]
]

Hash

rp({
  "one" => 1,
  two: {
    [1, 2] => {}
  }
})

Output:

{
  "one" => 1,
  two: {
    [
      1,
      2
    ] => {}
  }
}

Set

rp Set[1, "thing", Set[]]

Output:

Set[
  1,
  "thing",
  Set[]
]

Data

Direction = Data.define(:x, :y)
Velocity = Data.define(:speed, :direction)

rp Velocity[100, Direction[0.6, 0.8]]

Output:

Velocity[
  speed: 100,
  direction: Direction[
    x: 0.6,
    y: 0.8
  ]
]

Struct

Player = Struct.new(:health, :mana)

rp Player.new(100, 200)

Output:

Player( 0x298
  health: 100,
  mana: 200
)

Note that the object id is included for Struct but not for Data as the latter is a flyweight while the former is essentially an ordinary class.