Tag: stdout

从Ruby C扩展中抑制STDOUT

我在项目中使用gem dep_selector ,无法弄清楚如何从库的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输出。 有任何想法吗?

从Ruby运行交互式程序

我试图从ruby运行gnuplot(不使用外部gem)并解析其文本输出。 我尝试过IO.popen , PTY.spawn和Open3.popen3但每当我尝试获取输出时它就会“挂起” – 我想等待更多的输出来。 我觉得它以某种方式完成了使用Thread.new但我找不到正确的方法来实现它。 有谁知道它是如何完成的?

将全局$ stdout重新分配给console – ruby

我试图设置$ stdout暂时写入文件然后回到文件。 test.rb : old_stdout = $stdout $stdout.reopen(“mytestfile.out”,’w+’) puts “this goes in mytestfile” $stdout= old_stdout puts “this should be on the console” $stdout.reopen(“mytestfile1.out”,’w+’) puts “this goes in mytestfile1:” $stdout = old_stdout puts “this should be back on the console” 这是输出。 ruby test.rb => no output on the console cat mytestfile.out this goes in mytestfile this […]

我可以使用RSpec来模拟stdin / stdout来测试控制台的读写吗?

我的Ruby程序从stdin读取行并使用puts打印到stdout (终端)。 我可以使用RSpec来测试读写吗? 我可以将一个字符串注入我的程序,就像它是用stdin编写的那样,同时检查输出吗? line = STDIN.read.chomp.split 此外,我在循环中进行读写,直到line[0] “退出”。 我可以在循环运行时进行测试,还是应该调用subject.read_in和subject.write_out ?