Ruby-on-rails 使 rails 控制台输出更漂亮一点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14368218/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:18:14  来源:igfitidea点击:

Making the rails console output a little more pretty

ruby-on-railsrails-activerecord

提问by Bohn

a rails console output looks like this:

rails 控制台输出如下所示:

User.all
=> [#<User id: 1, name: "Michael Hartl", email: "[email protected]",
created_at: "2011-12-05 00:57:46", updated_at: "2011-12-05 00:57:46">,
#<User id: 2, name: "A Nother", email: "[email protected]", created_at:
"2011-12-05 01:05:24", updated_at: "2011-12-05 01:05:24">]

I was wondering if there is command that can make it easier to read? for example there was a .prettycommand in MongoDB console that was formatting the output a little more eye friendly. But not sure if there is something similar in Rails or not.

我想知道是否有可以使其更易于阅读的命令?例如,MongoDB 控制台中有一个.pretty命令,它对输出的格式更加友好。但不确定 Rails 中是否有类似的东西。

采纳答案by Leszek Andrukanis

Also you could use this incredible gem:

你也可以使用这个令人难以置信的宝石:

Awesome Print

很棒的打印

回答by valk

A bit more elegant shorthand:

更优雅的速记:

y User.all

回答by Ben

If you don't want to use a gem, here's the low rent version:

如果您不想使用 gem,这里是低租版本:

 puts User.all.to_yaml

回答by myhouse

I've been using pp. The pp stands for "pretty print." No Gem required.

我一直在用pp。pp 代表“漂亮的印刷品”。不需要宝石。

On rails console try doing this:

在 rails 控制台上尝试这样做:

pp User.all

You'll get each attributes and their value in the record display in a row instead a bundle of them if you simply do User.all.

如果您只执行 User.all,您将在记录显示中连续获得每个属性及其值,而不是一组它们。

Here's the documentation:

这是文档:

https://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html

https://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html

I am using Rails 5.1.3 and ruby 2.4.1p111 and it came already installed in my project. If this don't work, I imagine you have to do require 'pp'. I hope this helps.

我正在使用 Rails 5.1.3 和 ruby​​ 2.4.1p111,它已经安装在我的项目中。如果这不起作用,我想你必须这样做require 'pp'。我希望这有帮助。

回答by Anthony Alberto

You could try the awesome_print gem : https://github.com/michaeldv/awesome_print

你可以试试 awesome_print gem:https: //github.com/michaeldv/awesome_print

Once installed, you can pretty print any object using :

安装后,您可以使用以下命令漂亮地打印任何对象:

ap User.all

回答by jasonleonhard

Here are a few options

这里有几个选项

yaml format

yaml 格式

y your_code

awesome_print

真棒_打印

gem install awesome_print

Then in irb or pry

然后在 irb 或 pry

require 'awesome_print'
ap your_code

回答by Mirror318

Use pry

pry

Without pry:

不撬:

2.3.1 :001 > SupplierTerm.first
  SupplierTerm Load (39.4ms)  SELECT  "supplier_terms".* FROM "supplier_terms" ORDER BY "supplier_terms"."id" ASC LIMIT   [["LIMIT", 1]]
 => #<SupplierTerm id: "1bc48081-402a-41d9-b6af-d783c28bb363", 
entity_id: "927b398f-2bbd-40cb-b668-eb284e26688d", uses_custom_terms: 
false, requires_credit_check: false, requires_identity_check: false, 
requires_guarantees: true, requires_trade_reference_check: true, 
minimum_guarantees: 1, minimum_trade_references: 1, trade_account_limit: 
20000, created_at: "2017-02-01 22:11:49", updated_at: "2017-02-01 
22:11:49", created_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6", 
updated_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6", questions: [], 
minimum_approvers: 1, excluded_sources: nil> 

With pry:

用撬:

2.3.1 :002 > pry
[1] pry(main)> SupplierTerm.first
  SupplierTerm Load (0.4ms)  SELECT  "supplier_terms".* FROM "supplier_terms" ORDER BY "supplier_terms"."id" ASC LIMIT   [["LIMIT", 1]]
=> #<SupplierTerm:0x007fb4e1feff40
 id: "1bc48081-402a-41d9-b6af-d783c28bb363",
 entity_id: "927b398f-2bbd-40cb-b668-eb284e26688d",
 uses_custom_terms: false,
 requires_credit_check: false,
 requires_identity_check: false,
 requires_guarantees: true,
 requires_trade_reference_check: true,
 minimum_guarantees: 1,
 minimum_trade_references: 1,
 trade_account_limit: 20000,
 created_at: Wed, 01 Feb 2017 22:11:49 UTC +00:00,
 updated_at: Wed, 01 Feb 2017 22:11:49 UTC +00:00,
 created_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6",
 updated_by_id: "2c314f8a-6d84-48c8-a963-75130e97f1a6",
 questions: [],
 minimum_approvers: 1,
 excluded_sources: nil>

回答by Derrick Mar

There is an awesome gem called Jazz Hands. Includes pry-based enhancements, hirb, and awesome_print in rails console.

有一种很棒的宝石叫做Jazz Hands。在 rails 控制台中包括基于撬的增强、hirb 和 awesome_print。

P.S. You might want to use the fork Jazz Fingersto make it compatible with Ruby 2.1.2

PS 您可能想使用 fork Jazz Fingers使其与 Ruby 2.1.2 兼容