-
Notifications
You must be signed in to change notification settings - Fork 1
Operators
- (unary minus)
- (unary +) ~
! not
& (binary & or formal block arg)
- (binary minus) %
- (binary *) ** / (binary /) ^ |
- (binary +) << (left shift)
=~ !~
<
= <= <=> == != ===
= mlhs/mrhs
&&= ||=
|= %= &= **= *= ^= -= += /= <<=
=
&& || ? (ternary ?:) and or
defined?
Also for flipflops
.. ...
Broadly, the following table describes the precedence of Ruby operators.
No|Type/Associativity|Description|Operators
---|---|---|---|---
1|Binary left|Reference|. ::
2|Unary|General|! ~ +
3|Binary right|Power|**
4|Unary|Minus|-
5|Binary left|Products|* / %
6|Binary left|Additions|+ -
7|Binary left|Shifts|<< >>
8|Binary left|Bitwise and|&
9|Binary left|Bitwise or|| ^
10|Binary left (TODO why?)|Directional comparisons|> >= < <=
11|Binary none|General comparisons|<=> == === != =~ !~
12|Binary left|Logical and, argument form|&&
13|Binary left|Logical or, argument form|||
14|Binary left|Range constructors|.. ...
15|Ternary right|Logical ternary|?:
16|Unary|Defined|defined?
17|Binary none|Assignment modifier|rescue
18|Binary right|Assignments|= += -= etc.
19|Unary|Logical not, expression form|not
20|Binary left|Logical and/or, expression form|and or
21|Binary left|Statement modifiers|if unless while until rescue
Some special rules also apply:
- If a rescue modifier is applied to an assignment, e.g.
<lhs> = <rhs> rescue <value>,rescuebecomes an assignment modifier and takes precedence over the assignment operator (i.e. it will be interpreted as<lhs> = ( <rhs> rescue <value> )) according to the following rules: - The assignment operator may be simple or complex
- This does not apply to an assignment with a multiple left-hand-side, a multiple right-hand-side or a command call on the right-hand-side
- If the argument to
defined?ornot?is within parentheses and there is no whitespace preceding it, then the operator has the highest precedence (higher even than.and::)
A Ruby Language Reference
Copyright © by Michael Hore, 2016.
Introduction to This Document
Ruby Elements
- Classes and Modules
- Methods
- Blocks, Procs and Lambdas
- Execution Context and Closures
- Variables, Constants and Namespaces
- Types and Literals
- Ruby Expressions
- Operators
Syntax Grammar
Exceptions and Throw
Ruby Sourcefiles and Libraries
Multi Threading
Execution and Lifecycle