0

我正在尝试在 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
4

2 回答 2

0

You can't redirect output to a file inside a Ruby script using >>. That trick only works at the command-line.

To write to a file use:

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

See the documentation for pp for more information.

于 2013-01-28T19:35:22.273 回答
0

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

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
于 2013-01-28T23:25:23.603 回答