Operators

Table 2 lists all of Ruby’s operators in descending order of precedence. Operators that are implemented as methods may be overridden and are indicated in the Method column.

Table 2. Ruby’s operators

Operator

Description

Method

::

Scope resolution

 

[] []=

Reference, set

**

Raise to power (exponentiation)

+ - ! ˜

Positive (unary), negative (unary), logical negation, complement

✓ (not !)

* / %

Multiplication, division, modulo (remainder)

+ -

Addition, subtraction

<< >>

Shift left, shift right

&

Bitwise and

| ^

Bitwise or, bitwise exclusive or

> >= < <=

Greater than, greater than or equal to, less than, less than or equal to

<=> == === != =˜ !˜

Equality comparison (spaceship, equality, equality, not equal to, match, not match

✓ (not != or )

&&

Logical and

 

||

Logical or

 

.. ...

Range inclusive, range exclusive

✓ (not ...)

? :

Ternary

 

= += -= *= /= %= **= <<= >>= &= |= ^= &&= ||=

Assignment, abbreviated assignment

 

not

Logical negation

 

and or

Logical composition

 

defined?

Special operator (no precedence)

 

Get Ruby Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.