题
我想是这样的,看上去很好:
>> 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"
思想?
解决方案
在y
方法是一个方便的方式来获得一些非常YAML输出。
y ProductColor.all
假设你在script/console
作为jordanpg评论的,这个答案是过时的。对于3.2以上版本,你需要执行下面的代码,然后才能拿到y
方法工作的Rails:
YAML::ENGINE.yamler = 'syck'
在较旧的Ruby版本,即。 <= 1.9,SYCK仍但前提是,它 用Ruby 2.0.0的释放完全除去。
有关轨道4 /红宝石2可以使用刚刚
puts object.to_yaml
其他提示
您应该尝试 hirb 。这是对作出的红宝石控制台漂亮格式对象的瑰宝。您的脚本/控制台会话是这样的:
>> 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
您可以了解更多有关hirb其首页。
真棒打印也不错,如果你想要一个对象缩进。是这样的:
$ 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 /导轨/撬控制台整合它,添加到您的~/.irbrc
或~/.pryrc
文件:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
也指出,你可以使用:
j ProductColor.all.inspect
要JSON格式输出,而不是YAML
嗨,你还可以,如果
试试这个在您的脚本/控制台>> y ProductColor.all
不为你工作。
<强>尝试这样的:强>
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
,然后强>
>> y ProductColor.all
我有一些麻烦使它工作,所以我将添加我的两分钱到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的
使用irbtools
宝石。
它会自动格式化控制台输出,再加上你会得到吨的强大功能。
您可能希望定义ProductColor的检查方法返回你找到很多不错的东西。例如:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
其中后ProductColor.all的结果将显示为类似[<1 - 白色(白)>,...]。当然,你应该调整检查方法,以您的需求,以便它显示你在一个你喜欢的风格需要的所有信息。
编辑:另外,如果这个问题是在输出中缺少换行符,你可以尝试
require 'pp'
pp ProductColor.all
应插入换行符酌情
要添加以改变拉戈的建议使用AwesomePrint, 如果你不能/不应该/不希望将awesome_print宝石添加到您的项目的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'
(确保路径和版本是否正确,当然)
您也可以尝试对一组对象的
下面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
我认为该解决方案是最准确的。你应该试试这个:
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"
}
]