2012-10-19 50 views
5

根據我在網上所有的讀數,backticksystem之間的區別是什麼被返回。 backtick返回標準輸出,而system返回truefalse反向與紅寶石系統

我被告知他們都使用subshel​​l來執行操作。

但是我注意到另一個區別。

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

+3

答案看一看[5種方法從紅寶石運行命令(http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/);解釋了從ruby運行unix命令的各種方法的具體細節。 –

+0

@PrakashMurthy在提問之前我看了一下。那篇文章沒有提到任何有關例外的事情,都沒有提及主要計劃。謝謝。 –

+1

[在Ruby中執行命令](http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html)是一篇不錯的文章,講述異常處理。 –

回答