本文简要介绍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 Open3.capture2用法及代码示例
- Ruby Open3.capture2e用法及代码示例
- Ruby Open3.popen2e用法及代码示例
- Ruby Open3.popen3用法及代码示例
- Ruby Open3.popen2用法及代码示例
- Ruby Open3.pipeline用法及代码示例
- Ruby Open3.pipeline_rw用法及代码示例
- Ruby Open3.pipeline_w用法及代码示例
- Ruby Open3.pipeline_r用法及代码示例
- Ruby Open3.pipeline_start用法及代码示例
- Ruby OpenStruct.ostruct[name] =用法及代码示例
- Ruby OpenSSL.fips_mode =用法及代码示例
- Ruby OpenSSL模块用法及代码示例
- Ruby OpenRead.open用法及代码示例
- Ruby OpenSSL.print_mem_leaks用法及代码示例
- Ruby OpenURI模块用法及代码示例
- Ruby OpenStruct类用法及代码示例
- Ruby OpenSSL.Digest用法及代码示例
- Ruby OpenStruct.==用法及代码示例
- Ruby OpenStruct.each_pair用法及代码示例
- Ruby OpenStruct.delete_field用法及代码示例
- Ruby OpenStruct.dig用法及代码示例
- Ruby OpenStruct.new用法及代码示例
- Ruby OpenStruct.ostruct[name]用法及代码示例
- Ruby Option.level用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Open3.capture3。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。