【问题标题】:How to get nice formatting in the Rails console如何在 Rails 控制台中获得良好的格式
【发布时间】:2009-08-03 20:43:19
【问题描述】:

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

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

想法?

【问题讨论】:

    标签: ruby-on-rails irb


    【解决方案1】:

    y 方法是获得漂亮 YAML 输出的便捷方式。

    y ProductColor.all
    

    假设你在script/console

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

    YAML::ENGINE.yamler = 'syck'
    

    来自ruby-docs

    在旧的 Ruby 版本中,即。

    对于 rails 4/ruby 2,您可以只使用

    puts object.to_yaml
    

    【讨论】:

    • 这应该被标记为正确答案,因为它是内置的,可以立即使用,最重要的是,简单。
    • 这个答案已经过时了。请参阅:stackoverflow.com/questions/11571801/… 要完成这项工作,您必须先执行 YAML::ENGINE.yamler = 'syck'
    • 现在是 YAML::ENGINE.yamler = 'psych'
    • 这类似于 ryanb >> ProductColor.all >> y _
    • 就像上面说的@botbot,这是最好的答案,因为它适用于您无法访问.irbrc、其他控制台工具或控制台的其他配置的情况(例如,有限访问生产容器/服务器的合同开发人员)
    【解决方案2】:

    你应该试试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
    

    您可以通过 homepage 了解有关 hirb 的更多信息。

    【讨论】:

    • ryanb 的回答基本上就是我想要的,但这太酷了,不接受。
    • 虽然不是对原始问题的回答,但它可能会指出您可以将 hirb 内容添加到您的 ~/.irbrc 中,这样您就不必每次都要求并启用它。
    • 这个宝石现在已经过时了。
    • 有没有办法“轻松”对输出的列进行排序?我很想强制列 id 排在第一位,updated_at 和 created_at 排在最后(如果您在第一次迁移后添加列,updated_at 和 created_at 列不会在最后)
    【解决方案3】:

    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 => "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/rails/pry 控制台集成,请添加到您的 ~/.irbrc~/.pryrc 文件:

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

    【讨论】:

    • 我可以在 Rails 4 或 5 中使用这个 gem 吗?我在 github 页面中发现了以下注释: 注意:awesome_print v1.2.0 是支持 v1.9.3 之前的 Ruby 版本和 v3.0 之前的 Rails 版本的最后一个版本。即将推出的 awesome_print v2.0 将需要 Ruby v1.9.3 或更高版本以及 Rails v3.0 或更高版本。这是否意味着 gem 与这些版本已过时并导致冲突??
    【解决方案4】:
    >> puts ProductColor.all.to_yaml
    

    简单好用!

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

    【讨论】:

    • 这很好用!我无法获得其他投票最多的答案...我猜是因为我正在使用 ActiveResource(API 资源)
    【解决方案5】:

    还可以注意,你可以使用:

    j ProductColor.all.inspect
    

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

    【讨论】:

    • 这可能会失败,具体取决于 JSON/ruby 的版本,并且在无法拥有好东西的环境中可能需要漂亮的格式
    • 这会引发错误:JSON::GeneratorError: only generation of JSON objects or arrays allowed
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      您好,如果

      ,您也可以在脚本/控制台中尝试此操作
      >> y ProductColor.all
      

      不适合你。

      试试这个:

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

      那么

      >> y ProductColor.all
      

      【讨论】:

        【解决方案8】:

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

        gem 'awesome_print', require: 'ap'

        然后在

        rails console

        你可以的

        &gt; ap Model.all 就是这样。但是你也可以添加

        require "awesome_print"
        AwesomePrint.irb!
        

        到你的 ~/.irbrc,只要你打开控制台,就需要用这种方式 awesome_print,你可以简单地做

        模型.all 无需输入应用程序

        【讨论】:

          【解决方案9】:

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

          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

          【讨论】:

            【解决方案10】:

            使用irbtools 宝石。

            它会自动格式化控制台输出,而且你会得到很多很棒的功能。

            【讨论】:

            • 不错!但我无法让它格式化 ActiveResource 内容......除非我做错了什么
            【解决方案11】:

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

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

            之后 ProductColor.all 的结果将显示为 [, ...]。当然,您应该根据需要调整检查方法,以便它以您喜欢的样式显示您需要的所有信息。

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

            require 'pp'
            pp ProductColor.all
            

            应该在适当的地方插入换行符

            【讨论】:

            • 事实上require 'pp'rails console --sandbox 中是不可能的。出于某种原因,当我尝试要求 pp 时,我得到了 false。哎呀! rails console 中似乎已经默认需要 pp。我刚刚做了pp Model.connection_handler 并得到了漂亮的打印输出。谢谢。
            • @Green 如果require 返回false,则表示文件已经加载完毕。
            • 为什么只做ProductColor.all时不显示inspect
            【解决方案12】:

            要添加到 Alter Lago 对使用 AwesomePrint 的建议, 如果您不能/不应该/不想将 awesome_print gem 添加到项目的 Gemfile 中,请执行以下操作:

            gem install awesome_print

            编辑 ~/.irb.rc 并添加:

            $LOAD_PATH &lt;&lt; '/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'

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

            【讨论】:

              猜你喜欢
              • 2013-12-02
              • 1970-01-01
              • 2013-06-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-27
              • 1970-01-01
              • 2017-06-08
              相关资源
              最近更新 更多