Set up awesome_print as default Rails console formatter

awesome_print pretty prints/formats Ruby objects.

This post focuses on having awesome_print enabled in the rails console without changing/updating the Gemfile. This way it is available in all Rails applications and consoles.

You can always add the gem to the Gemfile and have this working for that specific rails application.

gem 'awesome_print', :require => 'ap'

To set it up, you need to install the gem

gem install awesome_print

To find the path for the gem. issue the following command

gem which awesome_print

If you use rbenv or rvm, you will need to add it to the load path in ~/.irbrc


$LOAD_PATH.unshift '/Users/gaurav/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/awesome_print-1.8.0/lib/'
require 'awesome_print'

With this done, you can use the AwesomePrint’s ap method to pretty print the Ruby objects in the console.

console> ap User.first

If you want to set up awesome_print as the default formatter in Rails console, you will need to call the following method in ~/.irbrc or ~/.pryrc if you use pry.

AwesomePrint.irb!

This way you won’t have to use the ap method to make the objects pretty. They will be awesome by default 🙂

console> User.first

That is it.

Enjoy pretty printing your Ruby objects.

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s