Ruby-on-rails 如何在 Rails 控制台中获得漂亮的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get nice formatting in the Rails console
提问by Tom Lehman
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?
想法?
回答by ryanb
The ymethod is a handy way to get some pretty YAML output.
该y方法是获得一些漂亮的 YAML 输出的便捷方法。
y ProductColor.all
Assuming you are in script/console
假设你在 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 ymethod to work:
正如 jordanpg 评论的那样,这个答案已经过时了。对于 Rails 3.2+,您需要执行以下代码才能使该y方法工作:
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.
在较旧的 Ruby 版本中,即。<= 1.9,仍然提供 Syck,但是随着 Ruby 2.0.0 的发布,它被完全删除。
For rails 4/ruby 2 you could use just
对于 rails 4/ruby 2 你可以只使用
puts object.to_yaml
回答by cldwalker
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:
你应该试试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
You can learn more about hirb at its homepage.
您可以在其主页上了解有关 hirb 的更多信息。
回答by Alter Lagos
Awesome printis 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 => "[email protected]",
: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 => "[email protected]",
:background_info => nil
}
]
To integrate it by default with your irb/rails/pry console, add to your ~/.irbrcor ~/.pryrcfile:
要将它默认与您的 irb/rails/pry 控制台集成,请添加到您的~/.irbrc或~/.pryrc文件中:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
回答by Rody
>> puts ProductColor.all.to_yaml
Simply works fine!
只是工作正常!
回答by davidcollom
May also be noted that you can use:
还可以注意到,您可以使用:
j ProductColor.all.inspect
to output in Json format rather than Yaml
以 Json 格式而不是 Yaml 格式输出
回答by AllenC
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
回答by AndreiMotinga
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
我在让它工作时遇到了一些麻烦,所以我将把我的两美分加到 awesome_print 中,将它添加到你的 Gemfile 中,最好是在 :development
gem 'awesome_print', require: 'ap'
gem 'awesome_print', require: 'ap'
then in
然后在
rails console
rails console
you can do
你可以做
> ap Model.allThat's it. However you can also add
> ap Model.all就是这样。但是你也可以添加
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
到你的 ~/.irbrc,这种方式在你打开控制台时都需要 awesome_print,你可以简单地做
Model.all without the need of typing ap
Model.all 无需输入 ap
回答by Abram
You may also try the following for a group of objects
您也可以针对一组对象尝试以下操作
Object.all.map(&:attributes).to_yaml
This will give you much niceroutput, like
这会给你更好的输出,比如
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
Calling to_yamlon attributes rather than the object itself saves you from viewing the full contents of the object in the output
调用to_yaml属性而不是对象本身可以避免在输出中查看对象的完整内容
Or puts Object.last.attributes.to_yamlfor a single object
或者puts Object.last.attributes.to_yaml对于单个对象
Shorthand is also available: y Object.last.attributes
速记也可用: y Object.last.attributes
回答by Peter Nguyen
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:
与 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"
}
]
回答by VivekVarade123
Use irbtoolsgem.
使用irbtools宝石。
It will automatically format the the console output plus you'll get tons of great features.
它将自动格式化控制台输出,并且您将获得大量强大的功能。

![Ruby-on-rails 使空白参数[] nil](/res/img/loading.gif)