English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

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.

optionsdescription
-awith -n or -p together can open the automatic split mode (auto split mode). Please see -n and -p option.
-cCheck syntax only, do not execute the program.
-C dirChange directory before execution (equivalent to -X).
-dEnable debug mode (equivalent to -debug).
-F patSpecify pat as the default split mode ($;).
-e progSpecify prog as the program to be executed on the command line. Multiple -e option to execute multiple programs.
-hDisplay an overview of the command line options.
-i [ ext]Rewrite the file content to the program output. The original file will be saved with the extension ext. If ext is not specified, the original file will be deleted.
-I dirAdd dir as the directory for loading libraries.
-K [ kcode]Specify the encoding of the multibyte character set. e or E corresponds to EUC (extended Unix code), s or S corresponds to SJIS(Shift-JIS), u or U correspond to UTF-8, a, A, n, or N correspond to ASCII.
-lEnable automatic line end processing. Remove a newline character from the input line and append 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 ($/) as octal. If octal is not specified, the default is \0.
-pPlace the code in an input loop. Output the value of the variable $_ after each iteration.
-r libuse require to load lib as the library before execution.
-sInterpret the matching pattern between the program name and file name parameters -xxx any parameters as switches, and define the corresponding variables.
-T [level]Set security level, perform purity test (if level is not specified, the default value is 1)
-vDisplay version and enable redundancy mode.
-wEnable redundancy mode. If the program file is not specified, read from STDIN.
-x [dir]Delete the text before the #!ruby line. If specified dirIf specified, change the directory 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 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 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