Reason #170 • June 19th, 2026

Tweaking inspect output with instance_variables_to_inspect

Yesterday we looked at Object#inspect and how it can be customized by overriding #inspect in our classes.

A completely new kid on the block, however, is the instance_variables_to_inspect method, which tweaks the default #inspect implementation to include only a specific subset of instance variables:

Ruby
class User
  def initialize(name, email, password)
    @name = name
    @email = email
    @password = password
  end

  private def instance_variables_to_inspect
    [:@name, :@email]
  end
end

puts User.new("Merry", "[email protected]", "secret").inspect
# => #&ltUser:0x0000000124b57438 @name="Merry", @email="[email protected]">
    

Note how the @password instance variable is not included in the output of #inspect, because it was omitted from the instance_variables_to_inspect method. Useful to avoid accidentally leaking sensitive information in logs or error messages!

A word of caution: The pp standard gem, which provides the pretty printing algorithm used by IRB to print expression results, does not have a released version that respects the new instance_variables_to_inspect method at the time of writing.

Code has already been merged in git, though, so it should be available soon™. In the meantime we can override #pretty_print_instance_variables to achieve the same effect:

Ruby
User.new("Merry", "[email protected]", "secret")
# => #&ltUser:0x0000000124b57438 @name="Merry", @email="[email protected]", @password="secret">

class User
  def pretty_print_instance_variables = instance_variables_to_inspect
end

User.new("Merry", "[email protected]", "secret")
# => #&ltUser:0x0000000124b57438 @name="Merry", @email="[email protected]">
    

Happy midsummer! 🌞

History

Object#instance_variables_to_inspect was added in Ruby 4.0.0, released Christmas 2025.

See the feature ticket at https://bugs.ruby-lang.org/issues/21219 for details.

Reason #171 ?