【问题标题】:How can I redirect pretty-print in IRB如何在 IRB 中重定向漂亮打印
【发布时间】:2013-01-28 19:32:12
【问题描述】:

我正在尝试重定向 IRB 中的漂亮打印输出,但 pp page >> results.txt 不起作用。 如何将漂亮的打印重定向到文件?我使用的是 Windows 操作系统。

我的代码

require 'nokogiri'
require 'mechanize'

agent = Mechanize.new

agent.user_agent_alias = 'Windows Mozilla'

page = agent.get('http://www.asus.com/Search/')
pp page

【问题讨论】:

    标签: ruby mechanize pretty-print


    【解决方案1】:

    您不能使用>> 将输出重定向到Ruby 脚本内的文件。该技巧仅适用于命令行。

    要写入文件,请使用:

    File.open('results.txt', 'a') { |fo| pp page, fo }
    

    有关更多信息,请参阅documentation pp

    【讨论】:

    • 由于某种原因,它给了我一个没有错误的空白 results.txt 文件,只是消息 #]
    【解决方案2】:

    好的,我得到它的工作,对于任何好奇的人,这是基于我发现的另一个漂亮的打印问题:

    require 'nokogiri'
    require 'mechanize'
    
    agent = Mechanize.new
    
    agent.user_agent_alias = 'Windows Mozilla'
    
    page = agent.get('http://www.asus.com/Search/')
    pp page
    
    File.open("results.txt","w") do |f|
    PP.pp(page,f)
    end
    

    【讨论】:

      猜你喜欢
      • 2011-10-29
      • 2017-05-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      相关资源
      最近更新 更多