How can remove multiline output with Rails console (Rails 6.1.7.2/Ruby 3.1.2)?

70 Views Asked by At

After upgrading to Rails 6.1.7.2 and Ruby 3.1.2, the way ActiveRecords are displayed in the output of the rails console is really annoying me. Basically, each record's attributes and values are displayed one line at a time. For example, Car.first outputs:

3.1.2 :001 > Car.first
  Car Load (7.0ms)  SELECT "cars".* FROM "cars" ORDER BY "cars"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => 
#<Car:0x000000010e8d6fd0
 id: 13,
 user_id: 1,
 track_id: 7,
 car_number: "979754",
 car_initial: "rofl",
 door_type: "Sealsafe",
 car_type: "Tri",
 shipper: "GMOT",
 pretrip_only: false,
 date_created: Sat, 16 Jul 2022 21:03:07.246098000 UTC +00:00,
 date_updated: nil,
 updated_by: nil,
 scoring_inspector_id: "65",
 scoring_rack_score_job_code_6036_why_made: "21",
 scoring_rack_score_stenciling_job_code_6037_why_made: "09",
 scoring_roof: "3",
 scoring_side_screens: "3",
 scoring_shear_bay_exterior: "4",
 scoring_exterior_door: "4",
 scoring_top_of_deck: "4",
 scoring_underside_of_deck: "4",
 scoring_side_posts_interior: "3",
 scoring_shear_bay_interior: "1",
 scoring_door_interior: "3",
 created_at: Wed, 20 Jul 2022 21:56:24.987715000 UTC +00:00,
 updated_at: Tue, 26 Jul 2022 21:03:07.290183000 UTC +00:00,
 expressyard_id: 1860065,
 last_expressyard_sync_attempt_error: "",
 last_expressyard_sync_attempt_params: nil,
 client_created_at: nil> 

This forces me to scroll way too much. Before I upgraded, I was using 2.7.3, and the entire record would be printed on one line, but my IDE (RubyMine) would wrap the text. How can I go back to the 2.7.3 style with 3.1.2?

2

There are 2 best solutions below

0
Robert Nubel On

https://github.com/rails/rails/pull/15172 introduced the new behavior, but the "verbose" style is becoming much more in-vogue these days so it may be a losing battle on your end to try and fight it. I'd personally suggest you give the value-per-line format more time, and it may grow on you!

But perhaps you could use AwesomePrint, or the more-maintained fork AmazingPrint, to customize your output and reach true debugging Nirvana.

# Gemfile
gem 'amazing_print'
# .irbrc
require "amazing_print"
AmazingPrint.irb!
# ~/.config/aprc
AmazingPrint.defaults = {
  multiline: false,
  ruby19_syntax: true
}

This produces output like:

irb(main):001:0> Bar.new
#<Bar:0x000000012da31050> { id: nil, thing: nil, t1: nil, t2: nil, t3: nil, t4: nil, t5: nil, t6: nil, t7: nil, t8: nil, t9: nil, this_is_a_really_long_attribute: nil, created_at: nil, updated_at: nil }

As compared to the prior:

irb(main):005:0> Bar.new
=>
#<Bar:0x00000001099cc7c8
 id: nil,
 thing: nil,
 t1: nil,
 t2: nil,
 t3: nil,
 t4: nil,
 t5: nil,
 t6: nil,
 t7: nil,
 t8: nil,
 t9: nil,
 this_is_a_really_long_attribute: nil,
 created_at: nil,
 updated_at: nil>
0
Alex On
# ~/.irbrc

IRB.conf[:INSPECT_MODE] = :p

Config setting shown here:
https://docs.ruby-lang.org/en/3.2/IRB.html#module-IRB-label-Configuration

Explained here:
https://docs.ruby-lang.org/en/3.2/IRB/Context.html#method-i-inspect_mode-3D

Which links to available inspectors - :pp, :yaml, :marshal:
https://docs.ruby-lang.org/en/3.2/IRB/Inspector.html

Docs, am I right:
https://github.com/ruby/irb/blob/v1.6.4/lib/irb/inspector.rb#L112

#                        vv
Inspector.def_inspector([:p, :inspect]){|v|
  Color.colorize_code(v.inspect, colorable: Color.colorable? && Color.inspect_colorable?(v))
}