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. |
-c | Only checks the syntax and does not execute the program. |
-C dir | Change directory before execution (equivalent to -X). |
-d | Enable debug mode (equivalent to -debug). |
-F pat | Specifies pat as the default detach mode ($;). |
-e prog | Specify prog as a program to be executed on the command line. Multiple -e options can be specified to execute multiple programs. |
-h | Displays 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 dir | Add 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. |
-l | Enable automatic end-of-line processing. Removes a newline character from the input line and appends a newline character to the output line. |
-n | Place 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. |
-p | Place the code inside an input loop. Outputs the value of variable $_ after each iteration. |
-r lib | Use require to load lib as a pre-execution library. |
-s | Interpret 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). |
-v | Display version and enable redundancy mode. |
-w | Enable 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 dir | Change directory before execution (equivalent to -C). |
-y | Enable parser debug mode. |
--copyright | Display copyright statement. |
--debug | Enable debug mode (equivalent to -d). |
--help | Display an overview of the command line options (equivalent to -h). |
--version | Display 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 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