system
> system("date")
jue 14 nov 2024 16:24:06 CET
=> true
This method calls a system program recived as string.
It will return true, false or nil. From Ruby 2.6, passing exception: true will raise an error instead of returning false or nil.
Global variable $? is set to a Process::Status object and it will contain information about the call itself, including the PID of the invoked process and the exit status.
> system("date")
jue 14 nov 2024 16:24:06 CET
=> true
> $?
=> #<Process::Status: pid 42487 exit 0>
backticks (``)
Call a system program and return its output.
> `date`
=> "jue 14 nov 2024 16:51:08 CET\n"
The global variable $? is set through the backticks too. With backticks you can also make use string interpolation.
%x()
It is an alternative to backticks style.
exec
Replaces the current process by doing one of the following:
-
Passing string command_line to the shell.
-
Invoking the executable at exe_path.
This method has potential security vulnerabilities if called with untrusted input
Open3.popen3
Allow you to take the control of the standar input and the standard error.
require 'open3'
Open3.popen3("curl http://example.com") do |stdin, stdout, stderr, thread|
pid = thread.pid
puts stdout.read.chomp
end
👉 Source