2012-05-17 39 views
0

我在project中使用gem dep_selector,并且无法弄清楚如何从库的C扩展中抑制stdout。从Ruby C扩展中抑制STDOUT

中的问题,我想压制的代码是在这里:

https://github.com/RiotGames/knife_cookbook_dependencies/blob/master/lib/kcd/shelf.rb#L26

我尝试这样做:

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_selector输出,当我运行该脚本。

任何想法?

回答

2

您可能可以从Rails中滑动一些代码,例如quietly方法,应该为您处理此问题。

内核#悄然使用以下沉默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