How to get nice formatting in the Rails console

前端 未结 12 1416
一生所求
一生所求 2020-12-12 10:50

I want to get something like this to look nice:

>> ProductColor.all
=> [#

        
相关标签:
12条回答
  • 2020-12-12 11:03

    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

    0 讨论(0)
  • 2020-12-12 11:06

    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
    
    0 讨论(0)
  • 2020-12-12 11:06

    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"
      }
    ]
    
    0 讨论(0)
  • 2020-12-12 11:07

    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
    
    0 讨论(0)
  • 2020-12-12 11:09

    May also be noted that you can use:

    j ProductColor.all.inspect
    

    to output in Json format rather than Yaml

    0 讨论(0)
  • 2020-12-12 11:11

    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)

    0 讨论(0)
提交回复
热议问题