Integrate awesome_print with Rails Console

Integrate awesome_print with Rails Console and Interactive Ruby -

awesome_print is code beautifying library available for beautifying Rails & Ruby code. This can be integrated with interactive Ruby and Rails console if you follow the instructions given below.

awesome_print :awesome-print-github This is available as open source on Github

Source : awesome_print

;

;

;

;

Option 1 - Integrate with Interactive Ruby

Step 1. Install

This gem can be installed with following command -

gem install awesome_print

This command will install the latest version of the gem awesome_print for you.

Step 2. Test

To test awesome_print, you need to start your Ruby environment and require awesome_print for your console,

irb(main):001:0> require 'awesome_print'
false
irb(main):002:0> test_data = {:a => 'Ruby in Rails String', :b => {:array => [1,2,3]}}
{:a => 'Ruby in Rails String', :b => {:array => [1,2,3]}}
irb(main):002:0> ap test_data
{
    :a => "Ruby in Rails String",
    :b => {
        :array => [
            [0] 1,
            [1] 2,
            [2] 3
        ]
    }
}

The ap (awesome_print) command prints the result by formatting & beautifying the argument data passed to it

Step 3. Set as default for irb

You can set awesome_print formatting as default formatting for your output by putting following lines in .irbrc file.

If .irbrc file has not been created yet, then create a new one.

Open irbrc file:

vi ~/.irbrc

Paste lines:

require "awesome_print"
AwesomePrint.irb!

Step 4. Restart

Then begin new terminal session and start interactive Ruby console. This will have awesome_print as the default output formatter for you.

Option 2 - Integrate with Rails application

Step 1. Add to Gemfile

Add the awesome_print gem in the Gemfile as given below in group development (Meaning you want to use this gem only for the development environment)

group :development do
   # some other gem
   gem awesome_print
end

This will use gem awesome_print for your Rails application.

Step 2. Test Rails console

When you start the Rails console using

rails console
Loading development environment (Rails 3.2.16)
irb(main):001:0> a = {:a =>[1,2,3], :b => 'Sample String'}
{
    :a => [
        [0] 1,
        [1] 2,
        [2] 3
    ],
    :b => "Sample String"
}

It will print output directly using the awesome_print library that you added in the group development.