concept backtick in category ruby

appears as: backticks, The backticks
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

14.6.1. The system and exec methods and backticks

The system method calls a system program. Backticks (``) call a system program and return its output. The exec method replaces the current process by returning an external command.

Calling system programs with backticks

To issue a system command with backticks, put the command between backticks. The main difference between system and backticks is that the return value of the backtick call is the output of the program you run:

>> d = `date`
=> "Sun Apr 22 08:49:11 EDT 2018\n"
>> puts d
Sun Apr 22 08:49:11 EDT 2018
=> nil
>> output = `cat`
I'm typing into cat. Since I'm using backticks,
I won't see each line echoed back as I type it.
Instead, cat's output is going into the
variable output.
=> "I'm typing into cat. Since I'm using backticks,\nI won't etc.
>> puts output
I'm typing into cat. Since I'm using backticks,
I won't see each line echoed back as I type it.
Instead, cat's output is going into the
variable output.

The backticks set $? just as system does. A call to a nonexistent method with backticks raises a fatal error:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage