14.1. Running External Programs

A language can’t be a glue language unless it can run external programs. Ruby offers more than one way to do this.

I can’t resist mentioning here that if you are going to run an external program, make sure that you know what that program does. I’m thinking about viruses and other potentially destructive programs. Don’t just run any old command string, especially if it came from a source outside the program. This is true regardless of whether the application is web-based.

14.1.1. Using system and exec

The system method (in Kernel) is equivalent to the C call of the same name. It will execute the given command in a subshell.

system("/usr/games/fortune")
# Output goes to stdout as usual...

Note that the second parameter, ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.