I have the following Ruby code:
system "clang test.c -o test"
system "./test"
When I execute the above Ruby code, the stdout is printed, but the stderr is not.
When I run clang test.c -o test && ./test
from the terminal, I get a segmentation fault
, but the Ruby script does not print this.
How can I get the Ruby script to print all output from the system command?
CodePudding user response:
The output "segmentation fault" is not printed by your test program itself. If you see this in your shell, it is in fact printed by the shell itself as a result of your program segfaulting.
Ruby does not print this. You can check the return code of your program after the system
cal returned however using the $?
variable which contains a Process::Status
object. Here, you can e.g. check for signaled?
to check if the process aborted due to a signal and then get this signal with the stopsig
method. The signal number for a segmentation fault is 11
.