【发布时间】:2018-03-07 14:36:20
【问题描述】:
我运行了一个脚本,它在标准输出上返回一个长 JSON,但显然它被截断了:
res = ''
Open3.popen2('node script.js') {|i,o,t|
while line = o.gets
res = res + line
end
}
puts res # it is truncated
为什么?如何避免?
【问题讨论】:
-
IO.gets在文件末尾调用时返回nil。我不知道Open3.popen2是如何处理流的,但看起来它认为node已经刷新了输出。 -
如果我们能重现这一点,将会有很大帮助。你能做一个minimal reproducible example吗?
-
为什么不只是
res = o.read? -
请注意,
res << line比res = res + line更有效,因为它附加到现有字符串而不是创建一个全新的字符串。