优先级¶ ↑
从高到低,这是 Ruby 的优先级表。高优先级的操作先于低优先级的操作发生。
!, ~, unary +
**
unary -
*, /, %
+, -
<<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, modifier-unless, modifier-while, modifier-until
{ } blocks
一元 + 和一元 - 用于 +1、-1 或 -(a + b)。
修饰符 if、修饰符 unless 等用于这些关键字的修饰符版本。例如,这是一个修饰符 unless 语句
a += 1 unless a.zero?
请注意,由于与优先级无关的原因,(a if b rescue c) 被解析为 ((a if b) rescue c)。请参阅 修饰符语句。
{ ... } 代码块的优先级低于所有列出的操作,但 do ... end 代码块的优先级更低。
优先级表中的所有其他单词都是关键字。