Ruby 的关键词(保留字)

发表时间

Ruby(3.2) 中使用了下列关键词(也成保留字):

ENCODING
当前文件的编码。参阅 Encoding
LINE
关键词在当前文件中的行号。
FILE
当前文件路径。
BEGIN
当前文件中最先运行的代码。参阅 miscellaneous syntax
END
当前文件中所有代码运行完之后再运行。参阅 miscellaneous syntax
alias
为两个方法之间创建一个别名(或者其它)。查阅 模块和类语法
and
优先值低于 && 的短路布尔值 and
begin
开始一个意外情况处理代码块。查阅 意外语法
break
跳离一个块。查阅 流控制表达式语法
case
开始 case 表达式。查阅 流控制表达式语法
class
创建或者打开一个类。查阅 模块和类的语法
def
定义一个方法。查阅 方法语法
defined?
返回一个描述参数的字符串。查阅 miscellaneous syntax
do
开始一个代码块。
else
caseifunless 表达式中受控条件之外的情况。查阅 流控制表达式
elsif
if 表达式的一个替代条件表达方式。查阅 流控制表达式
end
代码语法块的结束标志。用于类、模块、方法、意外处理和流控制。
ensure
Starts a section of code that is always run when an exception is raised. See exception handling
false
布尔值 false。查阅 字面量1(literal)
for
A loop that is similar to using the each method. See control expressions
if
Used for if and modifier if statements. See control expressions
in
Used to separate the iterable object and iterator variable in a for loop. See control expressions It also serves as a pattern in a case expression. See pattern matching
module
Creates or opens a module. See modules and classes syntax
next
Skips the rest of the block. See control expressions
nil
A false value usually indicating “no value” or “unknown”. See literals
not
Inverts the following boolean expression. Has a lower precedence than !
or
Boolean or with lower precedence than ||
redo
Restarts execution in the current block. See control expressions
rescue
Starts an exception section of code in a begin block. See exception handling
retry
Retries an exception block. See exception handling
return
Exits a method. See methods. If met in top-level scope, immediately stops interpretation of the current file.
self
The object the current method is attached to. See methods
super
Calls the current method in a superclass. See methods
then
Indicates the end of conditional blocks in control structures. See control expressions
undef
Prevents a class or module from responding to a method call. See modules and classes
unless
Used for unless and modifier unless statements. See control expressions
until
Creates a loop that executes until the condition is true. See control expressions
when
A condition in a case expression. See control expressions
while
Creates a loop that executes while the condition is true. See control expressions
yield
Starts execution of the block sent to the current method. See methods

总共 42 个词。也就是说,编写 Ruby 程序时,只需要也只能使用这些英文词。编程过程中,除了这些英文词以外,还会出现的英文词就是 Ruby 内置的方法了。

1 只是具有字面意义的变量。

作者