Skip to content
matiquePublic

About

A recursive Ruby object pretty printer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Wurm

Gem Version Downloads GitHub Build Ruby Style Guide MIT License

Wurm is a lightweight recursive pretty printer for Ruby objects.

It displays the structure of Ruby objects in a readable, indented format, including nested hashes, arrays, structs, and custom objects.

Wurm also detects circular references and shared object references, preventing infinite recursion and making complex object graphs easier to inspect.

Features

  • Recursive inspection of Ruby objects.
  • Support for Hash, Array, and Struct.
  • Support for custom objects through a wurm method.
  • Circular reference detection.
  • Shared object reference detection.
  • Object class and object ID information.
  • Special formatting for Date, DateTime, and Time.
  • Configurable output destination through an IO object.
  • No external runtime dependencies.

Installation

As usual:

# Gemfile
...
gem "wurm"
...

and run "bundle install".

Basic example

require "wurm"
...
Wurm.pp("Hello World")
Wurm.pp("legend", "Hello World")
...

Output:

--- "Hello World"

Hashes and arrays

data = {
  name: "Alice",
  age: 30,
  address: {
    city: "Berlin",
    country: "Germany"
  },
  hobbies: ["Ruby", "Programming"]
}

Wurm.pp(data)

Output:

--- <Hash ID: 0x5a8>
  name: "Alice"
  age: 30
  address: <Hash ID: 0x5b0>
    city: "Berlin"
    country: "Germany"
  hobbies: <Array ID: 0x5b8>
    [0] "Ruby"
    [1] "Programming"

Custom objects

Custom objects can define a wurm method that returns an array of attribute names to inspect.

class Person
  attr_accessor :name, :age, :address

  def initialize(name, age, address)
    @name = name
    @age = age
    @address = address
  end

  def wurm
    %i[name age address]
  end
end

person = Person.new(
  "Alice",
  30,
  { city: "Berlin", country: "Germany" }
)

Wurm.pp(person)

Wurm recursively displays nested structures and their values.

Output:

--- <Person ID: 0x5a8>
  name "Alice"
  age 30
  address <Hash ID: 0x5b0>
    city: "Berlin"
    country: "Germany"

Circular references

Wurm detects circular references using object identity. ruby

person = { name: "Alice" }
person[:friend] = person

Wurm.pp(person)

Instead of entering an infinite recursion, Wurm prints a reference to the previously visited object.

Output:

--- <Hash ID: 0x5a8>
  name: "Alice"
  friend: -> reference to --- <Hash ID: 0x5a8>

Shared references

Wurm also detects objects referenced from multiple locations.

shared = { value: 42 }

data = {
  first: shared,
  second: shared
}

Wurm.pp(data)

The second occurrence is displayed as a reference to the first occurrence.

Output:

--- <Hash ID: 0x5a8>
  first: <Hash ID: 0x5b0>
    value: 42
  second: -> reference to first: <Hash ID: 0x5b0>

Custom output destination

By default, output is written to $stdout.

You can provide another IO object:

require "stringio"

output = StringIO.new

Wurm.pp({ name: "Alice" }, io: output)

puts output.string

Supported types

Ruby type Handling
NilClass Scalar
TrueClass Scalar
FalseClass Scalar
Numeric Scalar
Symbol Scalar
String Scalar
Regexp Scalar
Time ISO 8601
Date ISO 8601
DateTime ISO 8601
Hash Recursive
Array Recursive
Struct Recursive
Custom objects Via wurm method

Miscellaneous

Copyright (c) 2026 Dittmar Krall (www.matiq.com), released under the MIT license.

About

A recursive Ruby object pretty printer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages