139

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?

sth
  • 222,467
  • 53
  • 283
  • 367
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272

12 Answers12

261

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
Andreas Lyngstad
  • 4,887
  • 2
  • 36
  • 69
ryanb
  • 16,227
  • 5
  • 51
  • 46
  • 5
    this should be marked as the right answer since it's built in, can be used immediately, and most of all, simple. – botbot May 03 '12 at 21:34
  • 15
    This answer is outdated. See: http://stackoverflow.com/questions/11571801/rails-console-y-helper-returns-nameerror-rather-than-yaml-formatting-output To make this work, you have to first execute `YAML::ENGINE.yamler = 'syck'`. – jordanpg Oct 02 '12 at 18:04
  • 5
    Its now YAML::ENGINE.yamler = 'psych' – jumpa Dec 05 '13 at 05:53
  • This is similar to ryanb >> ProductColor.all >> y _ – Deepak Lamichhane Sep 16 '14 at 21:04
  • 1
    like @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 Jan 17 '16 at 18:14
  • y 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]` – Eric Guo Nov 21 '18 at 03:36
104

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.

cldwalker
  • 6,155
  • 2
  • 27
  • 19
  • 4
    ryanb's answer is basically what I was looking for, but this is too cool not to accept. – Tom Lehman Aug 04 '09 at 17:46
  • 8
    While 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. – jordelver Oct 04 '10 at 12:13
  • 1
    This gem is outdated now . – Amrit Dhungana May 05 '14 at 12:13
  • Is 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) – MrYoshiji Aug 01 '16 at 15:48
35

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 => "info@hayesdubuque.com",
        :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 => "info@ziemannstreich.com",
        :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
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
  • 1
    Can 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?? – ltdev Feb 06 '17 at 15:17
13
>> puts ProductColor.all.to_yaml

Simply works fine!

Source: https://stackoverflow.com/a/4830096

Community
  • 1
  • 1
Rody
  • 131
  • 1
  • 2
  • This works great! I couldn't get the other top voted answers to work... I guess because I am using ActiveResource (API resources) – ckhatton Mar 25 '14 at 17:23
10

May also be noted that you can use:

j ProductColor.all.inspect

to output in Json format rather than Yaml

davidcollom
  • 413
  • 4
  • 11
  • this can fail depending on version of JSON/ruby, and the nice formatting may be needed in an environment where one cant have nice things – Todd Jan 17 '16 at 18:15
  • 3
    This raise error: JSON::GeneratorError: only generation of JSON objects or arrays allowed – Hassan Akram Sep 27 '16 at 09:14
9

I think this solution is the most accurate one. You should try this:

puts JSON.pretty_generate Entry.all.map(&:attributes)

This will give you a super nice output compare to YAML format:

[
  {
    "id": 44,
    "team_id": null,
    "member_id": 1000000,
    "match_id": 1,
    "created_at": "2019-04-09 15:53:14 +0900",
    "updated_at": "2019-04-09 15:53:14 +0900"
  },
  {
    "id": 45,
    "team_id": null,
    "member_id": 1000001,
    "match_id": 1,
    "created_at": "2019-04-09 15:53:36 +0900",
    "updated_at": "2019-04-09 15:53:36 +0900"
  },
  {
    "id": 46,
    "team_id": null,
    "member_id": 1000003,
    "match_id": 1,
    "created_at": "2019-04-09 15:56:40 +0900",
    "updated_at": "2019-04-09 15:56:40 +0900"
  },
  {
    "id": 47,
    "team_id": null,
    "member_id": 1000004,
    "match_id": 1,
    "created_at": "2019-04-09 15:56:48 +0900",
    "updated_at": "2019-04-09 15:56:48 +0900"
  }
]
Peter Nguyen
  • 349
  • 4
  • 13
8

Hi you can also try this in your script/console if

>> y ProductColor.all

not working for you.

Try this:

>> require 'yaml'

>> YAML::ENGINE.yamler = 'syck'

then

>> y ProductColor.all
AllenC
  • 2,754
  • 1
  • 41
  • 74
7

I had some troubles making it work so I'll add my two cents to awesome_print add this to your Gemfile, preferably in :development

gem 'awesome_print', require: 'ap'

then in

rails console

you can do

> ap Model.all That's it. However you can also add

require "awesome_print"
AwesomePrint.irb!

to your ~/.irbrc, this way awesome_print will be required anytime you open the console and you can simply do

Model.all without the need of typing ap

AndreiMotinga
  • 1,024
  • 17
  • 24
6

You may also try the following for a group of objects

Object.all.map(&:attributes).to_yaml

This will give you much nicer output, like

---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California

Calling to_yaml on attributes rather than the object itself saves you from viewing the full contents of the object in the output

Or puts Object.last.attributes.to_yaml for a single object

Shorthand is also available: y Object.last.attributes

Abram
  • 39,950
  • 26
  • 134
  • 184
5

Use irbtools gem.

It will automatically format the the console output plus you'll get tons of great features.

VivekVarade123
  • 3,564
  • 4
  • 24
  • 31
4

You might want to define ProductColor's inspect method to return something that you find nice. For example:

def inspect
  "<#{id} - #{name} (#{internal_name})>"
end

After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.

Edit: also if the issue was the lack of line breaks in the output, you might try

require 'pp'
pp ProductColor.all

which should insert linebreaks where appropriate

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • As a matter of fact `require 'pp'` isn't possible in `rails console --sandbox`. For some reason I get `false` when I try to require `pp`. Oops! it seems that `pp` is already required by default in `rails console`. I just did `pp Model.connection_handler` and got big pretty printed output. Thanks. – Green May 28 '13 at 18:10
  • @Green If `require` returns `false`, that just means that the file has been loaded already. – sepp2k May 28 '13 at 18:15
  • Why the `inspect` is not displayed when doing just `ProductColor.all`? – Arnold Roa Jun 27 '17 at 02:55
3

To add to Alter Lago's suggestion for using AwesomePrint, If you can't/shouldn't/don't want to add the awesome_print gem to your project's Gemfile, do this:

gem install awesome_print

Edit ~/.irb.rc and add this:

$LOAD_PATH << '/Users/your-user/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib'

require 'awesome_print'

(Making sure the path and version are correct, of course)

Excalibur
  • 3,258
  • 2
  • 24
  • 32