Backquotes

One other type of string deserves a special mention: a string enclosed by backquotes—that is, the inward-pointing quote character that is usually tucked away up toward the top-left corner of the keyboard: `.

Ruby considers anything enclosed by back-quotes to be a command that can be passed for execution by the operating system using a method such as print or puts. By now, you will probably already have guessed that Ruby provides more than one way of doing this. It turns out %x/some command/ has the same effect as `somecommand` and so does %x{some command}. On the Windows operating system, for example, each of the three lines shown next would pass the command dir to the operating system, causing a directory listing to be displayed:

4backquotes.rb ...

Get The Book of Ruby 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.