Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backtick vs system in ruby

Tags:

ruby

Base on all my readings on the web the difference between backtick and system is what is returned . backtick returns STDOUT while system returns true or false.

And I was told that both of them use subshell to perform the operation.

However I am noticing another difference.

output = system('aaa')
puts "output is: #{output}"

output = `aaa`
puts "output is: #{output}"

Result of above code is

$ ruby test.rb
output is: 
lab.rb:4:in ``': No such file or directory - aaa (Errno::ENOENT) from test.rb:4:in `<main>'

So it seems in the case of backtick the exceptions are raised to the main program. Operation system swallows the exception and the main program never sees the exception.

I'm using ruby 1.9.3 .

Is my analysis right ?

UPDATE: Got the answer. It's here https://gist.github.com/3730986 .

like image 259
Nick Vanderbilt Avatar asked Oct 19 '12 13:10

Nick Vanderbilt


People also ask

What is system in ruby?

System is a pure ruby interface to gather systems information from the current host. System offers a simple to use interface to gather an array of information including; OS, CPU, Filesystem etc...

What is ruby command?

Ruby command is a free and open source programming language; it is flexible and is feature rich. As the name suggests, ruby indeed is a jewel language which comes at a very low entry cost. Its plug and play capability and also easily readable syntax makes it very user-friendly.


1 Answers

Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:

Got the answer. It's here https://gist.github.com/3730986 .

~ answer per nodejs99

like image 145
DreadPirateShawn Avatar answered Oct 06 '22 01:10

DreadPirateShawn