如何在Rails控制台中获得良好的格式

时间:2009-08-03 20:43:19

标签: ruby-on-rails irb

我希望得到这样的东西看起来不错:

>> 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">]

这不起作用:

>> 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\">]"

这两点都没有:

>> 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"

思想?

12 个答案:

答案 0 :(得分:245)

y方法可以很方便地获得一些漂亮的YAML输出。

y ProductColor.all

假设你在script/console

正如jordanpg评论的那样,这个答案已经过时了。对于Rails 3.2+,您需要执行以下代码才能使y方法起作用:

YAML::ENGINE.yamler = 'syck'

来自ruby-docs

  

在较旧的Ruby版本中,即。 &lt; = 1.9,仍然提供Syck,但是它   随着Ruby 2.0.0的发布完全删除了。

对于rails 4 / ruby​​ 2,你可以只使用

puts object.to_yaml

答案 1 :(得分:92)

您应该尝试hirb。它是一个宝石,用于在ruby控制台中对漂亮的格式化对象。您的脚本/控制台会话将如下所示:

>> 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

您可以在homepage了解有关hirb的更多信息。

答案 2 :(得分:22)

如果你想要一个缩进的对象,

Awesome print也很好。类似的东西:

$ 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
    }
]

要默认将其与irb / rails / pry控制台集成,请添加到~/.irbrc~/.pryrc文件中:

require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc

答案 3 :(得分:10)

还可以注意到你可以使用:

j ProductColor.all.inspect

以Json格式输出而不是Yaml

答案 4 :(得分:10)

>> puts ProductColor.all.to_yaml

干得好!

来源:https://stackoverflow.com/a/4830096

答案 5 :(得分:8)

您好,您也可以在脚本/控制台中尝试此操作

>> y ProductColor.all

不适合你。

试试这个:

>> require 'yaml'

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

然后

>> y ProductColor.all

答案 6 :(得分:6)

我有一些麻烦使它工作,所以我将两美分加到awesome_print 将其添加到您的Gemfile中,最好是:development

gem 'awesome_print', require: 'ap'

然后在

rails console

你可以做到

> ap Model.all 就是这样。但是你也可以添加

require "awesome_print"
AwesomePrint.irb!

到你的〜/ .irbrc,这样你打开控制台时就会需要awesome_print,你可以这样做

Model.all 无需输入ap

答案 7 :(得分:5)

使用irbtools gem。

它会自动格式化控制台输出,而且你会得到很多很棒的功能。

答案 8 :(得分:4)

您可能希望定义ProductColor的inspect方法以返回您觉得不错的内容。例如:

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

之后,ProductColor.all的结果将显示为[&lt; 1 - White(White)&gt;,...]之类的内容。当然,您应该根据需要调整检查方法,以便以您喜欢的样式显示所需的所有信息。

编辑:如果问题是输出中缺少换行符,您可以尝试

require 'pp'
pp ProductColor.all

应该在适当的地方插入换行符

答案 9 :(得分:3)

要添加Alter Lago关于使用AwesomePrint的建议, 如果您不能/不应该/不想将awesome_print gem添加到项目的Gemfile中,请执行以下操作:

gem install awesome_print

编辑〜/ .irb.rc并添加:

$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'

(确保路径和版本正确无误)

答案 10 :(得分:3)

您也可以尝试以下一组对象

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

这将为您提供更好的输出,例如

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

在属性而不是对象本身上调用to_yaml可以使您无法在输出中查看对象的全部内容

puts Object.last.attributes.to_yaml单个对象

也可以使用速记:y Object.last.attributes

答案 11 :(得分:1)

我认为这种解决方案是最准确的。您应该尝试这样做:

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

与YAML格式相比,这将为您提供超棒的输出:

[
  {
    "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"
  }
]