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

发布于 2024-08-01 15:40:21 字数 2231 浏览 3 评论 0原文

我想让这样的东西看起来不错:

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

想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

又怨 2024-08-08 15:40:21

y 方法是获取一些漂亮的 YAML 输出的便捷方法。

y ProductColor.all

假设您在 script/console

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

YAML::ENGINE.yamler = 'syck'

来自 ruby-docs

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

对于 Rails 4/Ruby 2 你可以使用

puts object.to_yaml

The y method is a handy way to get some pretty YAML output.

y ProductColor.all

Assuming you are in 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 y method to work:

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.

For rails 4/ruby 2 you could use just

puts object.to_yaml
瞳孔里扚悲伤 2024-08-08 15:40:21

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

>> 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 的主页上了解有关 hirb 的更多信息。

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:

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

两人的回忆 2024-08-08 15:40:21

如果您想要缩进一个对象,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 => "[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
    }
]

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

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

Awesome print is 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 ~/.irbrc or ~/.pryrc file:

require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
回眸一遍 2024-08-08 15:40:21
>> puts ProductColor.all.to_yaml

简单地工作就很好!

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

>> puts ProductColor.all.to_yaml

Simply works fine!

Source: https://stackoverflow.com/a/4830096

月寒剑心 2024-08-08 15:40:21

还需要注意的是,您可以使用:

j ProductColor.all.inspect

以 Json 格式而不是 Yaml 格式输出

May also be noted that you can use:

j ProductColor.all.inspect

to output in Json format rather than Yaml

墨洒年华 2024-08-08 15:40:21

我认为这个解决方案是最准确的。 你应该尝试这个:

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

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:

[
  {
    "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"
  }
]
零崎曲识 2024-08-08 15:40:21

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

>> y ProductColor.all

您好,如果不适合您,

试试这个:

>> require 'yaml'

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

然后

>> y ProductColor.all

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
夜巴黎 2024-08-08 15:40:21

我在让它工作时遇到了一些麻烦,所以我将把我的两分钱添加到awesome_print
将其添加到您的 Gemfile 中,最好在 :development

gem 'awesome_print', require: 'ap'

中,然后在

rails console

中您可以执行

>> ap模型.all
就是这样。 不过,您也可以添加

require "awesome_print"
AwesomePrint.irb!

到 ~/.irbrc,这样每次打开控制台时都需要 Awesome_print,您只需执行

Model.all 即可
无需输入 ap

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

gem 'awesome_print', require: 'ap'

then in

rails console

you can do

> ap Model.all
That's it. However you can also add

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

Model.all
without the need of typing ap

酒儿 2024-08-08 15:40:21

您还可以对一组对象尝试以下操作

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 >

You may also try the following for a group of objects

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

This will give you much nicer output, like

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

Calling to_yaml on attributes rather than the object itself saves you from viewing the full contents of the object in the output

Or puts Object.last.attributes.to_yaml for a single object

Shorthand is also available: y Object.last.attributes

煮酒 2024-08-08 15:40:21

使用 irbtools gem。

它会自动格式化控制台输出,并且您将获得大量出色的功能。

Use irbtools gem.

It will automatically format the the console output plus you'll get tons of great features.

執念 2024-08-08 15:40:21

您可能想要定义 ProductColor 的检查方法以返回您认为不错的内容。 例如:

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

之后 ProductColor.all 的结果将显示为类似 [<1 - White (White)>, ...]。 当然,您应该根据您的需要调整检查方法,以便它以您喜欢的风格显示您需要的所有信息。

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

require 'pp'
pp ProductColor.all

在适当的位置插入换行符

You might want to define ProductColor's inspect method to return something that you find nice. For example:

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

After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.

Edit: also if the issue was the lack of line breaks in the output, you might try

require 'pp'
pp ProductColor.all

which should insert linebreaks where appropriate

紫竹語嫣☆ 2024-08-08 15:40:21

要添加 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'

(当然要确保路径和版本正确)

To add to Alter Lago's suggestion for using AwesomePrint,
If you can't/shouldn't/don't want to add the awesome_print gem to your project's Gemfile, do this:

gem install awesome_print

Edit ~/.irb.rc and add this:

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

(Making sure the path and version are correct, of course)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文