Ruby - String in Backquotes

Introduction

A string enclosed by backquotes:`

` is the inward-pointing quote character that is in 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.

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:

Demo

puts(`dir main.rb`) 

puts(%x/dir main.rb/) # from   w w w .j  av  a 2  s.  co  m
puts(%x{dir main.rb})

Result

Related Topic