【问题标题】:How to save console output to string in crystal?如何将控制台输出保存到水晶中的字符串?
【发布时间】:2017-03-10 00:30:19
【问题描述】:

在红宝石上我可以做到

require "stringio"
def with_captured_stdout
  begin
    old_stdout = $stdout
    $stdout = StringIO.new('','w')
    yield
    $stdout.string
  ensure
    $stdout = old_stdout
  end
end

后来这样称呼它

str = with_captured_stdout { Solution.main("Greetings from Javatlacati") }

但是在 上我可以引用全局变量$stdout 而不会得到相应的错误

不支持$global_variables,请改用@@class_variables

有什么解决方法吗?提前谢谢你。

【问题讨论】:

    标签: crystal-lang crystal-lang


    【解决方案1】:

    目前没有简单的方法可以在水晶标准库中捕获 stdout 或 stderr,但是这里有一个看起来可以做到这一点的分片:https://github.com/mosop/stdio

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多