Operators in Ruby
Learn arithmetic, comparison, logical, assignment, bitwise, ternary, and the spaceship operator with practical examples in Ruby.
Introduction to Operators
Operators are symbols that perform operations on values. In Ruby, most operators are actually method calls — you can even override them in your own classes.
1. Operators Are Methods
In Ruby, `+`, `-`, `*`, `==`, `<` and more are all methods defined on classes.
Arithmetic Operators
Ruby supports `+`, `-`, `*`, `/`, `%` (modulo), and `**` (exponentiation).
1. Basic Arithmetic
All standard math operators work on integers and floats.
2. divmod — Quotient and Remainder
`divmod` returns both the integer quotient and remainder in one call.
Assignment Operators
Ruby supports `=` and all compound assignment operators: `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `||=`, `&&=`.
1. Compound Assignment
Shorthand operators that read, modify, and write back in one step.
2. Logical Assignment (||=, &&=)
`||=` assigns only if nil/false; `&&=` assigns only if truthy.
Comparison Operators
Ruby provides `==`, `!=`, `<`, `>`, `<=`, `>=`, `<=>` (spaceship), and `===` (case equality).
1. Basic Comparison
`==`, `!=`, `<`, `>`, `<=`, `>=` work as expected.
2. Spaceship Operator (<=>)
`<=>` returns `-1`, `0`, or `1`. It is the foundation of all sorting in Ruby.
3. Case Equality (===)
`===` is used internally by `case/when`. Its behavior depends on the left-hand type.
Logical Operators
Ruby has two sets of logical operators: `&&`/`||`/`!` (high precedence) and `and`/`or`/`not` (low precedence, control-flow style).
1. &&, ||, !
`&&` (AND), `||` (OR), `!` (NOT) — high precedence, used in conditions.
2. and, or, not (Control Flow)
`and`/`or`/`not` have very low precedence and are intended for control flow, not conditions.
Conditional (Ternary) Operator
The ternary operator `condition ? value_if_true : value_if_false` returns one of two values based on a condition.
1. Ternary Operator
`condition ? a : b` — if condition is truthy, returns `a`, otherwise `b`.
Nil & Safe Navigation Operators
Ruby provides `||` for nil defaults, `&.` (safe navigation) to avoid NoMethodError on nil, and `nil?` to check for nil.
1. Safe Navigation Operator (&.)
`&.` calls a method only if the receiver is not nil — returns nil otherwise.
2. Nil Default with ||
Use `||` to provide a fallback when a value is nil or false.
Bitwise Operators
Ruby supports `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), and `>>` (right shift) for bitwise operations.
1. Bitwise AND, OR, XOR, NOT
These operators work bit by bit on the binary representation of integers.
2. Left Shift (<<) and Right Shift (>>)
`<<` multiplies by powers of 2; `>>` divides by powers of 2.
Operator Precedence
Ruby evaluates operators in a defined order. Higher-precedence operators are evaluated first. Use parentheses to make intent explicit.
1. Precedence Order (High to Low)
From highest to lowest: `**`, unary `!`/`~`/`+`, `*`/`/`/`%`, `+`/`-`, `<<`/`>>`, `&`, `|`/`^`, comparison, `==`/`!=`/`<=>`, `&&`, `||`, ternary, assignment, `not`, `and`/`or`.