liveBook · Manning

Command-line argument

As explained in The Well-Grounded Rubyist, Fourth Edition

A command-line option that instructs the Ruby interpreter to perform an action or change its behavior.

A command-line argument supplies the Ruby interpreter with a program filename or a switch that changes its behavior or tells it to perform an action. Multiple switches can be combined when starting Ruby. section 1.4.1

Common arguments support checking, diagnostics, and small scripts:

  • `-c` checks a file's syntax without executing it, while `-w` enables additional warnings. section 1.4.1
  • `-e` executes Ruby code supplied in quotation marks, making short scripts possible without creating a file. section 1.4.1
  • `-v` or `--version` displays Ruby version information; `-h` displays interpreter switch help. section 1.4.1

Run a one-line Ruby program

`ruby -e 'puts "David A. Black".reverse'` prints `kcalB .A divaD`. The quoted text is treated as the Ruby program rather than as a filename. section 1.4.1

Table 1.6 Commonly used Ruby command-line switches 

Switch

Description

Example of usage

-c

Checks the syntax of a program file without executing the program

ruby -c c2f.rb

-w

Gives warning messages during program execution

ruby -w c2f.rb

-e

Executes the code provided in quotation marks on the command line

ruby -e 'puts "Code demo!"'

-l

Line mode: prints a newline after every line of output

ruby -le 'print "+ newline!"'

-rname

Requires the named feature

ruby –rprofile

-v

Shows Ruby version information and executes the program in verbose mode

ruby –v

--version

Shows Ruby version information

ruby –-version

-h

Shows information about all command-line switches for the interpreter

ruby –h

Browse the switch table for compact descriptions and usage examples covering syntax checks, warnings, inline execution, line mode, requiring features, version information, and help. section 1.4.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains switch purposes and demonstrates inline execution, while stopping short of exhaustive switch reference.
Examples
Uses reversed-name scripts, syntax-check commands, warning flags, line mode, and version-check commands.
What it assumes
Assumes familiarity with starting Ruby from a command line and reading basic Ruby code.
What you can do after
Operate/configure Ruby from a shell by selecting switches for checking, warnings, execution, output, version display, and help.
How it connects
Connects command-line arguments with syntax checking, warning mode, inline scripting, version information, and interpreter help.

Also explained in

Compare how 14 books explain command-line argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.