How easy is it to hide results from system commands in ruby? For example, some of my scripts run
system "curl ..."
and I would not prefer to see the results of the download.
You can also catch the result. Exec is very limited in functionality and when executed will exit the Ruby program and run the command. The System command runs in a sub-shell instead of replacing the current process and returns true or nill.
There are a few Ruby methods you can use. Depending on the method you use you’ll get different results. Let’s explore these methods together! The Ruby system method is the simplest way to run an external command. Notice that system will print the command output as it happens. Also system will make your Ruby program wait until the command is done.
The systemmethod calls a system program. You have to provide the command as a string argument to this method. For example: >> system("date") Wed Sep 4 22:03:44 CEST 2013 => true The invoked program will use the current STDIN, STDOUTand STDERRobjects of your Ruby program. In fact, the actual return value is either true, falseor nil.
The system command can return a true if the command was successful or nill when not. echo "hello world" Will output "hello world" in the command window. You can also catch the result. Exec is very limited in functionality and when executed will exit the Ruby program and run the command.
You can use the more sophisticated popen3 to have control over STDIN, STDOUT and STDERR separately if you like:
Open3.popen3("curl...") do |stdin, stdout, stderr, thread|
# ...
end
If you want to silence certain streams you can ignore them, or if it's important to redirect or interpret that output, you still have that available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With