Tom*_*man 121 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">]
Run Code Online (Sandbox Code Playgroud)
这不起作用:
>> 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\">]"
Run Code Online (Sandbox Code Playgroud)
这也不是:
>> 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"
Run Code Online (Sandbox Code Playgroud)
思考?
rya*_*anb 248
该y方法是获得一些漂亮的YAML输出的便捷方法.
y ProductColor.all
Run Code Online (Sandbox Code Playgroud)
假设你在 script/console
正如jordanpg评论的那样,这个答案已经过时了.对于Rails 3.2+,您需要执行以下代码才能使y方法起作用:
YAML::ENGINE.yamler = 'syck'
Run Code Online (Sandbox Code Playgroud)
在较旧的Ruby版本中,即.<= 1.9,仍然提供Syck,但随着Ruby 2.0.0的发布,它已被完全删除.
对于rails 4/ruby 2,您可以使用
puts object.to_yaml
Run Code Online (Sandbox Code Playgroud)
cld*_*ker 94
你应该试试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
Run Code Online (Sandbox Code Playgroud)
您可以在其主页上了解有关hirb的更多信息.
Alt*_*gos 22
如果你想要一个缩进的对象,真棒印刷也很好.就像是:
$ 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
}
]
Run Code Online (Sandbox Code Playgroud)
在默认情况下你的IRB /导轨/撬控制台整合它,添加到您的~/.irbrc或~/.pryrc文件:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
Run Code Online (Sandbox Code Playgroud)
dav*_*lom 11
也可以注意到你可以使用:
j ProductColor.all.inspect
Run Code Online (Sandbox Code Playgroud)
以Json格式而不是Yaml输出
小智 11
>> puts ProductColor.all.to_yaml
Run Code Online (Sandbox Code Playgroud)
干得好!
资料来源:https://stackoverflow.com/a/4830096
我认为这个解决方案是最准确的。你应该试试这个:
puts JSON.pretty_generate Entry.all.map(&:attributes)
Run Code Online (Sandbox Code Playgroud)
与 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"
}
]
Run Code Online (Sandbox Code Playgroud)
嗨,您也可以在脚本/控制台中尝试这个
>> y ProductColor.all
Run Code Online (Sandbox Code Playgroud)
不适合你.
试试这个:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
Run Code Online (Sandbox Code Playgroud)
然后
>> y ProductColor.all
Run Code Online (Sandbox Code Playgroud)
我有一些麻烦使它工作,所以我将我的两美分添加到awesome_print添加到你的Gemfile,最好是 :development
gem 'awesome_print', require: 'ap'
然后在
rails console
你可以做
> ap Model.all
而已.但是你也可以添加
require "awesome_print"
AwesomePrint.irb!
Run Code Online (Sandbox Code Playgroud)
到你的〜/ .irbrc,这样一来打开控制台就会需要awesome_print,你可以简单地做
Model.all无需输入ap
您也可以针对一组对象尝试以下操作
Object.all.map(&:attributes).to_yaml
Run Code Online (Sandbox Code Playgroud)
这会给你更好的输出,比如
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
Run Code Online (Sandbox Code Playgroud)
调用to_yaml属性而不是对象本身可以避免在输出中查看对象的完整内容
或者puts Object.last.attributes.to_yaml对于单个对象
速记也可用: y Object.last.attributes
| 归档时间: |
|
| 查看次数: |
57597 次 |
| 最近记录: |