Ruby command line options


Ruby is generally run from the command line as follows:

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

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

Option Description
-a with -n or - p When used together, auto split mode can be turned on. See the -n and -p options.
-cOnly checks the syntax and does not execute the program.
-C dirChange directory before execution (equivalent to -X).
-dEnable debug mode (equivalent to -debug).
-F patSpecifies pat as the default detach mode ($;).
-e progSpecify prog as a program to be executed on the command line. Multiple -e options can be specified to execute multiple programs.
-hDisplays an overview of command line options.
-i [ ext]Rewrite the file contents as program output. The original file will be saved with the extension ext. If ext is not specified, the original file is deleted.
-I dirAdd dir as the directory to load the library.
-K [kcode]Specifies the multi-byte character set encoding. e or E corresponds to EUC (extended Unix code), s or S corresponds to SJIS (Shift-JIS), u or U corresponds to UTF-8, and a, A, n, or N corresponds to ASCII.
-lEnable automatic end-of-line processing. Removes a newline character from the input line and appends a newline character to the output line.
-nPlace the code in an input loop (just like in while gets; ... end ).
-0[ octal] Set the default record separator ($/) to octal. Defaults to \0 if octal is not specified.
-pPlace the code inside an input loop. Outputs the value of variable $_ after each iteration.
-r libUse require to load lib as a pre-execution library.
-sInterpret the matching pattern between the program name and file name parameters - any parameter of xxx as a switch, and define the corresponding variable.
-T [level]Set the security level and perform impurity testing (if level is not specified, the default value is 1).
-vDisplay version and enable redundancy mode.
-wEnable redundant mode. If no program file is specified, reads from STDIN.
-x [dir]Delete the text before the #!ruby line. If dir is specified, the directory is changed to dir.
-X dirChange directory before execution (equivalent to -C).
-yEnable parser debug mode.
--copyrightDisplay copyright statement.
--debugEnable debug mode (equivalent to -d).
--helpDisplay an overview of the command line options (equivalent to -h).
--versionDisplay version.
--verboseEnable verbose mode (equivalent to -v). Set $VERBOSE to true.
--yydebugEnable parser debug mode (equivalent to -y).

Single-character command line options can be used in combination. 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