当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Open3.capture3用法及代码示例


本文简要介绍ruby语言中 Open3.capture3 的用法。

用法

capture3(*cmd)

Open3.capture3 捕获命令的标准输出和标准错误。

stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts])

除了 opts[:stdin_data]opts[:binmode] 之外,参数 env、cmd 和 opts 被传递给 Open3.popen3 。请参阅 Process.spawn

如果指定了opts[:stdin_data],则将其发送到命令的标准输入。

如果opts[:binmode] 为真,则内部管道设置为二进制模式。

例子:

# dot is a command of graphviz.
graph = <<'End'
  digraph g {
    a -> b
  }
End
drawn_graph, dot_log = Open3.capture3("dot -v", :stdin_data=>graph)

o, e, s = Open3.capture3("echo abc; sort >&2", :stdin_data=>"foo\nbar\nbaz\n")
p o #=> "abc\n"
p e #=> "bar\nbaz\nfoo\n"
p s #=> #<Process::Status: pid 32682 exit 0>

# generate a thumbnail image using the convert command of ImageMagick.
# However, if the image is really stored in a file,
# system("convert", "-thumbnail", "80", "png:#{filename}", "png:-") is better
# because of reduced memory consumption.
# But if the image is stored in a DB or generated by the gnuplot Open3.capture2 example,
# Open3.capture3 should be considered.
#
image = File.read("/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png", :binmode=>true)
thumbnail, err, s = Open3.capture3("convert -thumbnail 80 png:- png:-", :stdin_data=>image, :binmode=>true)
if s.success?
  STDOUT.binmode; print thumbnail
end

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Open3.capture3。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。