-1

I’m using Rails 4.2. I have this in my Gemfile …

gem 'awesome_print', '~>1.8'

This is how it appears in Gemfile.lock

awesome_print (1.8.0)

However, when I log in to the rails console, objects are not printed out in a pretty form

$ rails c
Loading development environment (Rails 4.2.10)
irb: warn: can't alias context from irb_context.
(dev)> User.last
  User Load (0.7ms)  SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
=> #<User id: 88836, dob: "1960-10-02", first_name: “Test”, email: “test@test.com, created_at: "2021-10-01 14:11:40", updated_at: "2021-10-01 14:11:46", middle_name: nil, last_name: “Test”, active: true, nick_name: nil, …

I’m not sure what else I need to do to get a pretty printout of objects in the Rails console. I thought including the gem would take care of this for me.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • 1
    If you want to pretty print in your console you need to add "ap" (obviously for awesome print) as a prefix to your object. So here it should be "ap User.last". Make sure you go through the docs next time ;) – askprod Oct 01 '21 at 15:07
  • Maybe I need to revise my question or start a new one, but is there any way to make the printing pretty by default? That is, without having to add any special prefixes? – Dave Oct 01 '21 at 16:10
  • " I thought including the gem would take care of this for me." - not by default, no. But you can set your irb/pry to use awesome_print for formatting. It's covered in the readme. – Sergio Tulentsev Oct 01 '21 at 16:13

1 Answers1

0

As mentioned in comments, per the README -- https://github.com/awesome-print/awesome_print, editing ~/.irbrc by adding these lines

require "awesome_print"
AwesomePrint.irb!

does the job for setting awesome_print to be the default for IRB (rails console).

Dave
  • 15,639
  • 133
  • 442
  • 830