如何在 Ruby 中更改 FFI 调用的 STDOUT?
我正在通过 FFI 访问 Ruby 中的 C 函数。该函数称为AllocTilts::summary
。
我希望该方法不将任何内容打印到 STDOUT。但是我的 STDOUT 临时重定向不起作用。我还能做些什么吗?
puts 'test outside before' #prints successfully
File.open("/var/alloc_tilts/summary_dump", "w") do |out|
stdout, $stdout = $stdout, out
puts 'test inside' #doesn't print to STDOUT as expected
AllocTilts.summary(2012, 2011) #prints undesired stuff to STDOUT
$stdout = stdout
end
puts 'test outside after' #prints successfully
I am accessing a C function in Ruby through FFI. The function is called AllocTilts::summary
.
I want the method to not print anything to STDOUT. However my temporary redirect of STDOUT is not working. Is there something else I can do?
puts 'test outside before' #prints successfully
File.open("/var/alloc_tilts/summary_dump", "w") do |out|
stdout, $stdout = $stdout, out
puts 'test inside' #doesn't print to STDOUT as expected
AllocTilts.summary(2012, 2011) #prints undesired stuff to STDOUT
$stdout = stdout
end
puts 'test outside after' #prints successfully
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AllocTilts.summary
如何写入标准输出?如果它使用printf
并且您无权访问其源代码,则您无能为力(除了调用 libcdup
)。ruby 写入
$stdout
而不是真正的 stdout 的方式是,您应该使用类似的代码在
AllocTilts.summary
内生成输出。How do
AllocTilts.summary
write to stdout? If it usesprintf
and you do not have access to its source code there is nothing you can do (short of calling libcdup
).The way ruby writes to
$stdout
rather than the real stdout, isYou should use similar code to generate output inside
AllocTilts.summary
.