Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Command-Line Options

Ruby Command-Line Options

Ruby is generally run from the command line as follows:


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

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

Option Description
-a When used with -n or -p, enables auto split mode. See the -n and -p options.
-c Check syntax only, without executing the program.
-C dir Change directory before execution (equivalent to -X).
-d Enable debug mode (equivalent to -debug).
-F pat Specify pat as the default split pattern ($;).
-e prog Specify prog as the program to execute on the command line. Multiple -e options can be given to execute multiple programs.
-h Display an overview of command-line options.
-i [ ext] Rewrite file contents with program output. The original file is saved with the extension ext appended. If ext is not specified, the original file is deleted.
-I dir Add dir as a directory for loading libraries.
-K [ kcode] Specify the multibyte character set encoding. e or E corresponds to EUC (extended Unix code), s or S to SJIS (Shift-JIS), u or U to UTF-8, and a, A, n, or N to ASCII.
-l Enable automatic line-ending processing. Remove one newline from input lines and append a newline to output lines.
-n Place the code inside an input loop (as in while gets; ... end).
-0[ octal] Set the default record separator ($/) to the specified octal value. If octal is not specified, it defaults to \0.
-p Place the code inside an input loop. After each iteration, output the value of $_.
-r lib Use require to load lib as a library before execution.
-s Interpret any arguments between the program name and filename arguments that match the pattern -xxx as switches and define the corresponding variables.
-T [level] Set the security level and perform taint checks (if level is not specified, the default is 1).
-v Display the version and enable verbose mode.
-w Enable verbose mode. If no program file is specified, read from STDIN.
-x [dir] Delete text before the #!ruby line. If specified, dir, change the directory to dir。
-X dir Change directory before execution (equivalent to -C).
-y Enable parser debug mode.
--copyright Display the copyright notice.
--debug Enable debug mode (equivalent to -d).
--help Display an overview of command-line options (equivalent to -h).
--version Display the version.
--verbose Enable verbose mode (equivalent to -v). Set $VERBOSE to true.
--yydebug Enable parser debug mode (equivalent to -y).

Single-character command-line options can be combined. The following two lines mean the same thing:


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


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