A possible approach is to change your C ++ program so that it contributes its input from the standard input stream (std :: cin) instead of command line parameters and returns its result through standard output (std :: cout) instead of the main return value. Your Ruby script will then use popen to run the C ++ program.
Assuming the C ++ program looks like this:
// *pseudo* code int main(int argc, char* argv[]) { large_data_file = expensive_operation(); std::vector<int> input = as_ints(argc, argv); int result = make_the_computation(large_data_file, input); return result; }
It will be converted to something like:
// *pseudo* code int main(int argc, char* argv[]) { large_data_file = expensive_operation(); std::string input_line; // Read a line from standard input while(std:::getline(std::cin, input_line)){ std::vector<int> input = tokenize_as_ints(input_line); int result = make_the_computation(large_data_file, input); //Write result on standard output std::cout << result << std::endl; } return 0; }
And the Ruby script will look like
io = IO.popen("./myprogram", "rw") while i_have_stuff_to_compute arguments = get_arguments()
source share