本文簡要介紹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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。