ruby 预定义 常量 & 变量

本文详细介绍了Ruby语言中的预定义变量与常量,包括它们的名称、用途及默认值等信息。对于Ruby开发者来说,掌握这些内置特性有助于更好地理解和使用该语言。

Ruby Predefined Variables

Variable NameDescription
$!The last exception object raised. The exception object can also be accessed using => in rescue clause.
$@The stack backtrace for the last exception raised. The stack backtrace information can retrieved by Exception#backtrace method of the last exception.
$/The input record separator (newline by default). gets, readline, etc., take their input record separator as optional argument.
$\The output record separator (nil by default).
$,The output separator between the arguments to print and Array#join (nil by default). You can specify separator explicitly to Array#join.
$;The default separator for split (nil by default). You can specify separator explicitly for String#split.
$.The number of the last line read from the current input file. Equivalent to ARGF.lineno.
$<Synonym for ARGF.
$>Synonym for $defout.
$0The name of the current Ruby program being executed.
$$The process pid of the current Ruby program being executed.
$?The exit status of the last process terminated.
$:Synonym for $LOAD_PATH.
$DEBUGTrue if the -d or --debug command-line option is specified.
$defoutThe destination output for print and printf ($stdout by default).
$FThe variable that receives the output from split when -a is specified. This variable is set if the -a command-line option is specified along with the -p or -n option.
$FILENAMEThe name of the file currently being read from ARGF. Equivalent to ARGF.filename.
$LOAD_PATHAn array holding the directories to be searched when loading files with the load and require methods.
$SAFEThe security level
  • 0 --> No checks are performed on externally supplied (tainted) data. (default)

  • 1 --> Potentially dangerous operations using tainted data are forbidden.

  • 2 --> Potentially dangerous operations on processes and files are forbidden.

  • 3 --> All newly created objects are considered tainted.

  • 4 --> Modification of global data is forbidden.

$stdinStandard input (STDIN by default).
$stdoutStandard output (STDOUT by default).
$stderrStandard error (STDERR by default).
$VERBOSETrue if the -v, -w, or --verbose command-line option is specified.
$- xThe value of interpreter option -x (x=0, a, d, F, i, K, l, p, v). These options are listed below
$-0The value of interpreter option -x and alias of $/.
$-aThe value of interpreter option -x and true if option -a is set. Read-only.
$-dThe value of interpreter option -x and alias of $DEBUG
$-FThe value of interpreter option -x and alias of $;.
$-iThe value of interpreter option -x and in in-place-edit mode, holds the extension, otherwise nil. Can enable or disable in-place-edit mode.
$-IThe value of interpreter option -x and alias of $:.
$-lThe value of interpreter option -x and true if option -lis set. Read-only.
$-pThe value of interpreter option -x and true if option -pis set. Read-only.
$_The local variable, last string read by gets or readline in the current scope.
$~The local variable, MatchData relating to the last match. Regex#match method returns the last match information.
$ n ($1, $2, $3...)The string matched in the nth group of the last pattern match. Equivalent to m[n], where m is a MatchData object.
$&The string matched in the last pattern match. Equivalent to m[0], where m is a MatchData object.
$`The string preceding the match in the last pattern match. Equivalent to m.pre_match, where m is a MatchData object.
$'The string following the match in the last pattern match. Equivalent to m.post_match, where m is a MatchData object.
$+The string corresponding to the last successfully matched group in the last pattern match.

Ruby Predefined Constants

Constant NameDescription
TRUESynonym for true.
FALSESynonym for false.
NILSynonym for nil.
ARGFAn object providing access to virtual concatenation of files passed as command-line arguments or standard input if there are no command-line arguments. A synonym for $<.
ARGVAn array containing the command-line arguments passed to the program. A synonym for $*.
DATAAn input stream for reading the lines of code following the __END__ directive. Not defined if __END__ isn't present in code.
ENVA hash-like object containing the program's environment variables. ENV can be handled as a hash.
RUBY_PLATFORMA string indicating the platform of the Ruby interpreter.
RUBY_RELEASE_DATEA string indicating the release date of the Ruby interpreter
RUBY_VERSIONA string indicating the version of the Ruby interpreter.
STDERRStandard error output stream. Default value of $stderr.
STDINStandard input stream. Default value of $stdin.
STDOUTStandard output stream. Default value of $stdout.
TOPLEVEL_BINDINGA binding object at Ruby's top level.

转载于:https://my.oschina.net/xieyunzi/blog/292255

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值