How do I start an external program (like an excel sheet) from ruby and wait for its execution resp. termination before continuing.
I know I can start the excel sheet with
system 'start excel "my/path/to/the/sheet"'
but using this will only start the sheet and then continues.
Do not use start! The command system will wait for the result. In Windows prompt the start command starts programs asynchronously.
Or you can use %x too, if you want an array as result :
If you have the start command in your command, %x will wait for the output anyway..
As already stated, dropping the "start" will cause the Ruby script to wait.
Another way to do it in Ruby is with backticks.
However, you pointed out Excel as an example. If you bring up a regular Windows command prompt, you will notice that while
start excel path\to\sheet
will open Excel whereas justexcel path\to\sheet
will not. You will get an error about how "excel" is not a recognized internal or external command. The way to fix this is to either add the path to Excel into your Environment Variables or to include the path to Excel in yoursystem()
call.(Using the backticks here is just my preference. I prefer it since it enables the variable insertion.) This will bring up an instance of Excel and the Ruby script will wait for the application's termination.
The problem you are having is not with Ruby but the start command, this launches another program and returns immediately. You need to make that command wait for excel to finish using the
wait
flag: