title: Communicating between a ruby script and a running c++ program
body: <p>I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so:</p>
  
  <pre><code>$ ./myprogram 1 2 3 4 5 6 7
  </code></pre>
  
  <p>I also have a ruby script, and I would like this script to utilize the c++ program.
  Currently, I am doing this like so.</p>
  
  <p>Ruby Code:</p>
  
  <pre><code>arguments = "1 2 3 4 5 6 7"
  an_integer = %x{ ./myprogram #{arguemnts} }
  puts "The program returned #{an_integer}" #=&gt; The program returned 2283
  </code></pre>
  
  <p>This is all working properly, but my problem is that each time ruby makes this call, the c++ program has to reload the data-file (which is over 100mb) - very slow, and very inefficient.  </p>
  
  <p>How can I rewrite my c++ program load the file only once, allowing me to make many lookups via a ruby script without reloading the file each time. Would using sockets be a sensible approach? Writing the c++ program as a ruby extension?</p>
  
  <p>Obviously I am not an experienced c++ programmer, so thanks for your help.</p>