1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/doc/syntax/precedence.rdoc
Jeremy Evans 29c1e9a0d4 Document the difference between expressions and statements [ci skip]
In the grammar, all expressions are statements, but not all
statements are expressions.  Some parts of the grammar accept
expressions and not other types of statements, which causes
similar looking code to parse differently due to operator
precedence.

Mostly from Dan0042 (Daniel DeLorme).

Fixes [Bug #16092]
2019-10-10 13:45:19 -07:00

64 lines
1.1 KiB
Text

= Precedence
From highest to lowest, this is the precedence table for ruby. High precedence
operations happen before low precedence operations.
!, ~, unary +
**
unary -
*, /, %
+, -
<<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, modifier-unless, modifier-while, modifier-until
{ } blocks
Unary <code>+</code> and unary <code>-</code> are for <code>+1</code>,
<code>-1</code> or <code>-(a + b)</code>.
Modifier-if, modifier-unless, etc. are for the modifier versions of those
keywords. For example, this is a modifier-unless statement:
a += 1 unless a.zero?
Note that <code>(a if b rescue c)</code> is parsed as <code>((a if b) rescue
c)</code> due to reasons not related to precedence. See {modifier
statements}[control_expressions_rdoc.html#label-Modifier+Statements].
<code>{ ... }</code> blocks have priority below all listed operations, but
<code>do ... end</code> blocks have lower priority.
All other words in the precedence table above are keywords.