位置:首页 > 脚本语言 > Ruby在线教程 > Ruby命令行选项

Ruby命令行选项

Ruby一般是在命令行中运行以下方式:

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

解释器可以调用任何下列选项来控制环境和行为的解释。

选项 描述
-a Used with -n or -p to split each line. Check -n and -p options.
-c Checks syntax only, without executing program.
-C dir Changes directory before executing (equivalent to -X).
-d Enables debug mode (equivalent to -debug).
-F pat Specifies pat as the default separator pattern ($;) used by split.
-e prog Specifies prog as the program from the command line. Specify multiple -e options for multiline programs.
-h Displays an overview of command-line options.
-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.
-I dir Adds dir as the directory for loading libraries.
-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 UTF-8; and a, A, n, or N for ASCII).
-l Enables automatic line-end processing. Chops a newline from input lines and appends a newline to output lines.
-n Places code within an input loop (as in while gets; ... end).
-0[ octal] Sets default record separator ($/) as an octal. Defaults to \0 if octal not specified.
-p Places code within an input loop. Writes $_ for each iteration.
-r lib Uses require to load lib as a library before executing.
-s Interprets any arguments between the program name and filename arguments fitting the pattern -xxx as a switch and defines the corresponding variable.
-T [level] Sets the level for tainting checks (1 if level not specified).
-v Displays version and enables verbose mode
-w Enables verbose mode. If programfile not specified, reads from STDIN.
-x [dir] Strips text before #!ruby line. Changes directory to dir before executing if dir is specified.
-X dir Changes directory before executing (equivalent to -C).
-y Enables parser debug mode.
--copyright Displays copyright notice.
--debug Enables debug mode (equivalent to -d).
--help Displays an overview of command-line options (equivalent to -h).
--version Displays version.
--verbose Enables verbose mode (equivalent to -v). Sets $VERBOSE to true
--yydebug Enables parser debug mode (equivalent to -y).

单字符命令行选项可以结合起来。下面两行表达同一个意思:

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


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