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

Unary + and unary - are for +1, -1 or -(a + b).

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 (a if b rescue c) is parsed as ((a if b) rescue c) due to reasons not related to precedence. See modifier statements.

Blocks are not part of the precedence table above; the difference between the two block forms is how far they reach. A { ... } block binds to the nearest method call, while a do ... end block binds to the outermost one, so { ... } binds more tightly than do ... end:

p [1, 2].map { |x| x * 2 }      # prints [2, 4]
p [1, 2].map do |x| x * 2 end   # prints an Enumerator

All other words in the precedence table above are keywords.