【问题标题】:Why doesn't Ruby's IRB print_print correctly?为什么 Ruby 的 IRB print_print 不正确?
【发布时间】:2014-05-15 17:14:36
【问题描述】:

使用Rails 4.0.4Ruby 2.1.1,在模型控制器内部,我想使用pp 来检查对象。我打电话给debugger 暂停执行并环顾四周。但是,pp 似乎截断了输出,不允许它们出现在多行:

>pp ShopOrder

#<Shoporder id: 64, order_id: "142949823232-3232353652342", shop_id: 1, order_
status: "Completed", order_tests_queued: "2014-04-02 18:19:36", shipped_time: 
nil, shipping_address_name: "John Doe", total: 43.9, created_time: "2014-04-02
03:04:20", raw: "{\"order_id\":\"142949823232-3232353652342\",\"order_sta...">

 ( ^^ I did not truncated this for the question, it just ends this way ^^ )

我习惯于 pp 通常很好地格式化它并在多行上显示它,更类似于使用 y ShopOrder 获得格式化良好的 to_yaml 输出时发生的情况:

--- !ruby/object:Shoporder
attributes:
  id: 64
  order_id: 142949823232-3232353652342
  shop_id: 1
  order_status: Completed
  order_tests_queued: 2014-04-02 18:19:36.498044000 Z
  shipped_time: 
  shipping_address_name: John Doe
  total: 43.9
  created_time: 2014-04-02 03:04:20.000000000 Z
  ... (output continues correctly but has been truncated for this example )

irb_context.echo 返回true 时启用回声

yto_yaml 很好,但有时我也希望能够使用 pp

为什么没有按预期工作?

【问题讨论】:

    标签: ruby-on-rails ruby pretty-print irb


    【解决方案1】:

    如果内存服务于pp,则其输出依赖于pretty_print 方法。也许某些东西正在你的模型中定义它,或者覆盖它并截断super返回的字符串。

    【讨论】:

    • 我是 ruby​​ 新手 - 我该如何调试呢?在 bash 中,我知道您可以使用类似 \curl (带有斜杠)来防止使用别名...
    • 艰难的... :-|就个人而言,我会搜索我的项目以及我使用的各种宝石来查找pretty_print,并从最可能的罪魁祸首开始逐一检查它们。 (诚​​然,可能有更好的方法。)
    【解决方案2】:

    改用 Awesome Print

    这是(奇怪的)大型对象的预期行为。

    不要使用来自pretty_printpp 使用来自awesome_printap

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2011-08-18
      相关资源
      最近更新 更多