[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 | # Gemfile |
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 | # ~/.irbrc |
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 | require "amazing_print" |
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 | # config/environments/development.rb |
AmazingPrint Default Settings
You can set your own default options by assign your defaults to AmazingPrint.defaults
.
1 | AmazingPrint.defaults = { |
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 | require "amazing_print" |
Use ap
within Rails console.
1 | rails> ap Account.limit(2).all |
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 | logger.ap object, :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
[2] amazing_print | RubyGems.org | your community gem host - https://rubygems.org/gems/amazing_print/
[4] awesome_print | RubyGems.org | your community gem host - https://rubygems.org/gems/awesome_print/