5
根據我在網上所有的讀數,backtick
和system
之間的區別是什麼被返回。 backtick
返回標準輸出,而system
返回true
或false
。反向與紅寶石系統
我被告知他們都使用subshell來執行操作。
但是我注意到另一個區別。
output = system('aaa')
puts "output is: #{output}"
output = `aaa`
puts "output is: #{output}"
結果的上面的代碼是
$ ruby test.rb
output is:
lab.rb:4:in ``': No such file or directory - aaa (Errno::ENOENT) from test.rb:4:in `<main>'
所以看起來在異常提升到主程序backtick
的情況。操作system
吞下異常並且主程序從不會看到異常。
我使用紅寶石1.9.3。
我的分析是否正確?
更新:得到了答案。它在這裏https://gist.github.com/3730986。
答案看一看[5種方法從紅寶石運行命令(http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/);解釋了從ruby運行unix命令的各種方法的具體細節。 –
@PrakashMurthy在提問之前我看了一下。那篇文章沒有提到任何有關例外的事情,都沒有提及主要計劃。謝謝。 –
[在Ruby中執行命令](http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html)是一篇不錯的文章,講述異常處理。 –