How to get nice formatting in the Rails console
Solution 1
The y
method is a handy way to get some pretty YAML output.
y ProductColor.all
Assuming you are in script/console
As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y
method to work:
YAML::ENGINE.yamler = 'syck'
From ruby-docs
In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was completely removed with the release of Ruby 2.0.0.
For rails 4/ruby 2 you could use just
puts object.to_yaml
Solution 2
You should try hirb. It's a gem made to to pretty format objects in the ruby console. Your script/console session would look like this:
>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> ProductColor.first
+----+-------+---------------+---------------------+---------------------+
| id | name | internal_name | created_at | updated_at |
+----+-------+---------------+---------------------+---------------------+
| 1 | White | White | 2009-06-10 04:02:44 | 2009-06-10 04:02:44 |
+----+-------+---------------+---------------------+---------------------+
1 row in set
=> true
You can learn more about hirb at its homepage.
Solution 3
Awesome print is nice too if you want an object indented. Something like:
$ rails console
rails> require "awesome_print"
rails> ap Account.all(:limit => 2)
[
[0] #<Account:0x1033220b8> {
:id => 1,
:user_id => 5,
:assigned_to => 7,
:name => "Hayes-DuBuque",
:access => "Public",
:website => "http://www.hayesdubuque.com",
:toll_free_phone => "1-800-932-6571",
:phone => "(111)549-5002",
:fax => "(349)415-2266",
:deleted_at => nil,
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
:email => "[email protected]",
:background_info => nil
},
[1] #<Account:0x103321ff0> {
:id => 2,
:user_id => 4,
:assigned_to => 4,
:name => "Ziemann-Streich",
:access => "Public",
:website => "http://www.ziemannstreich.com",
:toll_free_phone => "1-800-871-0619",
:phone => "(042)056-1534",
:fax => "(106)017-8792",
:deleted_at => nil,
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
:email => "[email protected]",
:background_info => nil
}
]
To integrate it by default with your irb/rails/pry console, add to your ~/.irbrc
or ~/.pryrc
file:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
Solution 4
>> puts ProductColor.all.to_yaml
Simply works fine!
Source: https://stackoverflow.com/a/4830096
Solution 5
May also be noted that you can use:
j ProductColor.all.inspect
to output in Json format rather than Yaml
Comments
-
Tom Lehman almost 2 years
I want to get something like this to look nice:
>> ProductColor.all => [#<ProductColor id: 1, name: "White", internal_name: "White", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 2, name: "Ivory", internal_name: "Ivory", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 3, name: "Blue", internal_name: "Light Blue", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 4, name: "Green", internal_name: "Green", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">]
This doesn't work:
>> ProductColor.all.inspect => "[#<ProductColor id: 1, name: \"White\", internal_name: \"White\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 2, name: \"Ivory\", internal_name: \"Ivory\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 3, name: \"Blue\", internal_name: \"Light Blue\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 4, name: \"Green\", internal_name: \"Green\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">]"
And neither does this:
>> ProductColor.all.to_yaml => "--- \n- !ruby/object:ProductColor \n attributes: \n name: White\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"1\"\n internal_name: White\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Ivory\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"2\"\n internal_name: Ivory\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Blue\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"3\"\n internal_name: Light Blue\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Green\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"4\"\n internal_name: Green\n attributes_cache: {}\n\n"
Thoughts?
-
Tom Lehman over 14 yearsryanb's answer is basically what I was looking for, but this is too cool not to accept.
-
jordelver over 13 yearsWhile not an answer to the original question, it might be pointing out that you can add the hirb stuff to your ~/.irbrc so you don't have to require and enable it each time.
-
botbot almost 12 yearsthis should be marked as the right answer since it's built in, can be used immediately, and most of all, simple.
-
jordanpg over 11 yearsThis answer is outdated. See: stackoverflow.com/questions/11571801/… To make this work, you have to first execute
YAML::ENGINE.yamler = 'syck'
. -
Green almost 11 yearsAs a matter of fact
require 'pp'
isn't possible inrails console --sandbox
. For some reason I getfalse
when I try to requirepp
. Oops! it seems thatpp
is already required by default inrails console
. I just didpp Model.connection_handler
and got big pretty printed output. Thanks. -
sepp2k almost 11 years@Green If
require
returnsfalse
, that just means that the file has been loaded already. -
jumpa over 10 yearsIts now YAML::ENGINE.yamler = 'psych'
-
Crimbo about 10 yearsThis works great! I couldn't get the other top voted answers to work... I guess because I am using ActiveResource (API resources)
-
Crimbo about 10 yearsNice! But I can't get it to format ActiveResource content... unless I am doing something wrong
-
Amrit Dhungana almost 10 yearsThis gem is outdated now .
-
Deepak Lamichhane over 9 yearsThis is similar to ryanb >> ProductColor.all >> y _
-
Todd over 8 yearslike @botbot said above, this is the best answer since it applies to situations where you ahve no access to an
.irbrc
, other console tools, or other configurations of the console (like, being a contracted dev with limited access to a production container/server) -
Todd over 8 yearsthis can fail depending on version of JSON/ruby, and the nice formatting may be needed in an environment where one cant have nice things
-
MrYoshiji over 7 yearsIs there a way to "easily" sort the columns of the output? I would love to force the column id to be first and updated_at & created_at at the end (if you add columns after the first migration the updated_at & created_at columns will not be at the end)
-
Hassan Akram over 7 yearsThis raise error: JSON::GeneratorError: only generation of JSON objects or arrays allowed
-
ltdev about 7 yearsCan I use this gem with Rails 4 or 5 ?? I've found the following note in the github page: NOTE: awesome_print v1.2.0 is the last release supporting Ruby versions prior to v1.9.3 and Rails versions prior to v3.0. The upcoming awesome_print v2.0 will require Ruby v1.9.3 or later and Rails v3.0 or later. Does this mean that the gem is outdated with these versions and cause conflicts??
-
Arnold Roa almost 7 yearsWhy the
inspect
is not displayed when doing justProductColor.all
? -
Eric Guo over 5 yearsy method coming from ruby and still available in 2.5.
irb(main):010:0> method(:y).source_location => ["/usr/local/Cellar/ruby/2.5.3_1/lib/ruby/2.5.0/psych/y.rb", 5]