[Awesome Ruby Gem] Use amazing_print or awesome_print gem to print Ruby objects in full color exposing their internal structure with proper indentation

amazing_print or awesome_print

AmazingPrint is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.

It is a fork of AwesomePrint - https://github.com/awesome-print/awesome_print which became stale and should be used in its place to avoid conflicts.

Recommend to use amazing_print gem according to the RubyGem latest update:

GitHub latest update RubyGem latest update
amazing_print Now 1.2.2 - October 12, 2020
awesome_print Now 2.0.0.pre2 - January 24, 2019

Installation

You can install it as a gem:

1
$ gem install amazing_print

or add it into a Gemfile (Bundler):

1
2
3
4
5
6
7
8
# Gemfile

# Put gems used only for development or testing in the appropriate group in the Gemfile
group :development do
# amazing-print/amazing_print: Pretty print your Ruby objects with style -- in full color and with proper indentation
# https://github.com/amazing-print/amazing_print
gem 'amazing_print', `1.2.2'
end

Then, run bundle install.

1
$ bundle install

Configuration

Integration

IRB Integration

To use amazing_print as default formatter in irb and Rails console add the following code to your ~/.irbrc or ./irbrc file:

1
2
3
# ~/.irbrc
require "amazing_print"
AmazingPrint.irb!

PRY Integration

If you miss amazing_print’s way of formatting output, here’s how you can use it in place of the formatting which comes with pry. Add the following code to your ~/.pryrc or ./.pryrc file:

1
2
require "amazing_print"
AmazingPrint.pry!

Rails Console Integration

If you just want ot use amazing_print’s way of formatting output in the Rails console, add the following code to your config/environments/development.rb file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config/environments/development.rb

Rails.application.configure do

# ...

console do
# require "awesome_print"
# AwesomePrint.irb!

require "amazing_print"
AmazingPrint.irb!
end

# ...

AmazingPrint Default Settings

You can set your own default options by assign your defaults to AmazingPrint.defaults.

1
2
3
4
5
6
7
AmazingPrint.defaults = {
:indent => -2,
:color => {
:hash => :whiteish,
:class => :white
}
}

Or create ~/.aprc file in your home directory or ./.aprc file in your project directory. Within that file assign your defaults to AmazingPrint.defaults.

Usage

Use ap to print object within irb or pry.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require "amazing_print"

> a = [1, 2]
=> [1, 2]

# Print array.
> ap a
[
[0] 1,
[1] 2
]

> h = {a: 1, b: 2}
=> {:a=>1, :b=>2}

# Print hash.
> ap h
{
:a => 1,
:b => 2
}
=> nil

Use ap within Rails console.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
rails> ap Account.limit(2).all
[
[0] #<Account:0x1033220b8> {
:id => 1,
:user_id => 5,
:assigned_to => 7,
:name => "Hayes-DuBuque",
:access => "Public",
:website => "http://www.hayesdubuque.com",
:toll_free_phone => "1-800-932-6571",
:phone => "(111)549-5002",
:fax => "(349)415-2266",
:deleted_at => nil,
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
:email => "[email protected]",
:background_info => nil
},
[1] #<Account:0x103321ff0> {
:id => 2,
:user_id => 4,
:assigned_to => 4,
:name => "Ziemann-Streich",
:access => "Public",
:website => "http://www.ziemannstreich.com",
:toll_free_phone => "1-800-871-0619",
:phone => "(042)056-1534",
:fax => "(106)017-8792",
:deleted_at => nil,
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
:email => "[email protected]",
:background_info => nil
}
]
rails> ap Account
class Account < ActiveRecord::Base {
:id => :integer,
:user_id => :integer,
:assigned_to => :integer,
:name => :string,
:access => :string,
:website => :string,
:toll_free_phone => :string,
:phone => :string,
:fax => :string,
:deleted_at => :datetime,
:created_at => :datetime,
:updated_at => :datetime,
:email => :string,
:background_info => :string
}

Logger Convenience Method

amazing_print adds the ‘ap’ method to the Logger and ActiveSupport::BufferedLogger classes letting you call:

1
logger.ap object

By default, this logs at the :debug level. You can override that globally with:

1
:log_level => :info

in the custom defaults (see below). You can also override on a per call basis with:

1
2
3
4
logger.ap object, :warn

# or
logger.ap object, level: :warn

You can also pass additional options (providing nil or leaving off level will log at the default level):

1
logger.ap object, { level: :info, sort_keys: true }

Visit Usage - https://github.com/amazing-print/amazing_print#usage to learn more examples.

References

[1] amazing-print/amazing_print: Pretty print your Ruby objects with style – in full color and with proper indentation - https://github.com/amazing-print/amazing_print

[2] amazing_print | RubyGems.org | your community gem host - https://rubygems.org/gems/amazing_print/

[3] awesome-print/awesome_print: Pretty print your Ruby objects with style – in full color and with proper indentation - https://github.com/awesome-print/awesome_print

[4] awesome_print | RubyGems.org | your community gem host - https://rubygems.org/gems/awesome_print/