Ruby - Command Line Options


Advertisements


Ruby is generally run from the command line in the following way −

$ ruby [ options ] [.] [ programfile ] [ arguments ... ]

The interpreter can be invoked with any of the following options to control the environment and behavior of the interpreter.

Sr.No. Option & Description
1

-a

Used with -n or -p to split each line. Check -n and -p options.

2

-c

Checks syntax only, without executing program.

3

-C dir

Changes directory before executing (equivalent to -X).

4

-d

Enables debug mode (equivalent to -debug).

5

-F pat

Specifies pat as the default separator pattern ($;) used by split.

6

-e prog

Specifies prog as the program from the command line. Specify multiple -e options for multiline programs.

7

-h

Displays an overview of command-line options.

8

-i [ ext]

Overwrites the file contents with program output. The original file is saved with the extension ext. If ext isn't specified, the original file is deleted.

9

-I dir

Adds dir as the directory for loading libraries.

10

-K [ kcode]

Specifies the multibyte character set code (e or E for EUC (extended Unix code); s or S for SJIS (Shift-JIS); u or U for UTF8; and a, A, n, or N for ASCII).

11

-l

Enables automatic line-end processing. Chops a newline from input lines and appends a newline to output lines.

12

-n

Places code within an input loop (as in while gets; ... end).

13

-0[ octal]

Sets default record separator ($/) as an octal. Defaults to \0 if octal not specified.

14

-p

Places code within an input loop. Writes $_ for each iteration.

15

-r lib

Uses require to load lib as a library before executing.

16

-s

Interprets any arguments between the program name and filename arguments fitting the pattern -xxx as a switch and defines the corresponding variable.

17

-T [level]

Sets the level for tainting checks (1 if level not specified).

18

-v

Displays version and enables verbose mode.

19

-w

Enables verbose mode. If program file not specified, reads from STDIN.

20

-x [dir]

Strips text before #!ruby line. Changes directory to dir before executing if dir is specified.

21

-X dir

Changes directory before executing (equivalent to -C).

22

-y

Enables parser debug mode.

23

--copyright

Displays copyright notice.

24

--debug

Enables debug mode (equivalent to -d).

25

--help

Displays an overview of command-line options (equivalent to h).

26

--version

Displays version.

27

--verbose

Enables verbose mode (equivalent to -v). Sets $VERBOSE to true.

28

--yydebug

Enables parser debug mode (equivalent to -y).

Single character command-line options can be combined. The following two lines express the same meaning −

$ruby -ne 'print if /Ruby/' /usr/share/bin
   
$ruby -n -e 'print if /Ruby/' /usr/share/bin

ruby_environment.htm

Advertisements