Reputation: 189
How can you run a C++ program from a Ruby script?
Suppose that the Ruby script generates a file, called "hello.txt" and I want to run a C++ program to take the hello.txt file, work with it and write another file, called "result.txt" and the Ruby script continues to read the result.txt file.
For example, in the Linux shell I wrote g++ hello.c -c hello -o hello
to receive the "result.txt" file.
Is there is a way that I can run the shell code from a Ruby program?
Upvotes: 0
Views: 1206
Reputation: 2103
I find that backticks are more succinct than system
.
You can trigger the C++ program by shelling out as follows:
`./hello file.txt`
Can you clarify whether you need to read results.txt from the current directory?
If so, you could use something like contents = IO::readlines './results.txt'
Upvotes: 1
Reputation: 11274
You can use system
like other people said, however you should also check the exit value to verify the success or failure.
r = system("g++ hello.c -c hello -o hello") #=> r = true if success, nil if failed
Upvotes: 3