从Ruby C扩展中抑制标准输出

从Ruby C扩展中抑制标准输出,ruby,stdout,stderr,Ruby,Stdout,Stderr,我在a中使用gem,不知道如何从库的C扩展中抑制标准输出 我要抑制的代码如下所示: 我试过这个: real_stdout = $stdout $stdout = StringIO.new real_stderr = $stderr $stderr = StringIO.new puts "This gets suppressed correctly" selector.find_solution( ... ) # still prints to the terminal 但是当我运行脚本时,

我在a中使用gem,不知道如何从库的C扩展中抑制标准输出

我要抑制的代码如下所示:

我试过这个:

real_stdout = $stdout
$stdout = StringIO.new
real_stderr = $stderr
$stderr = StringIO.new
puts "This gets suppressed correctly"
selector.find_solution( ... ) # still prints to the terminal
但是当我运行脚本时,仍然会得到dep_选择器输出


有什么想法吗?

您可以从Rails中删除一些代码,比如方法,这些代码应该可以帮您解决

Kernel#悄悄地使用以下命令使STDOUT和STDERR静音

# Silences any stream for the duration of the block.
#
#   silence_stream(STDOUT) do
#     puts 'This will never be seen'
#   end
#
#   puts 'But this will'
def silence_stream(stream)
  old_stream = stream.dup
  stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
  stream.sync = true
  yield
ensure
  stream.reopen(old_stream)
end