Month: April 2021

ruby – difference between system, exec and backticks

There are multiple ways to run system commands from Ruby.

They are system, using backticks (‵), using %x(), using exec, or the more advanced option of using Open3. All of them behave in different ways.

system

The system method calls a system program. You have to provide the command as a string argument to this method. For example:

>> system("date")
Sat  4/10/2021   9:11:38
=> true

Note: In Windows, the date command also takes the input to change the system date after displaying the current system date. Press Enter to continue

The invoked program will use the current STDIN, STDOUT and STDERR objects of your Ruby program. In fact, the actual return value is either true, false or nil. In the example the date was printed through the IO object of STDIN. The method will return true if the process exited with a zero status, false if the process exited with a non-zero status and nil if the execution failed.

Continue reading

Copyright © 2024 the möbius trip

Theme by Anders NorenUp ↑