Operators in Ruby
Learn arithmetic, comparison, logical, assignment, and string operators in Ruby with practical Docker-ready examples
Operators are the verbs of a programming language—they let you add numbers, compare values, combine strings, and make decisions. Ruby gives you the familiar set of arithmetic and comparison operators you’d expect, plus a few elegant extras like the spaceship operator (<=>) and conditional assignment (||=).
What makes Ruby fascinating is a detail that flows directly from its object-oriented core: operators are actually methods. When you write 2 + 3, Ruby is really calling the + method on the integer 2 with 3 as the argument. This consistency—where even + is just a message sent to an object—is part of what gives Ruby its expressive, malleable character. It also means you can define operators on your own classes.
In this tutorial you’ll work through Ruby’s operator categories with runnable examples, then see how the “operators are methods” idea plays out, including the handy safe navigation operator (&.) for working with nil.
Arithmetic, Comparison, Logical, and Assignment Operators
This first example tours the everyday operators. Note that integer division (a / b) truncates toward zero, while introducing a float makes the whole expression floating point.
Create a file named operators.rb:
| |
Operators Are Methods
Because Ruby is purely object-oriented, every operator is a method call you could also write explicitly. This is not just trivia—it means you can give operators meaning on your own objects by defining methods like + or <=>. This example also shows the safe navigation operator &., which returns nil instead of raising an error when the receiver is nil.
Create a file named operators_methods.rb:
| |
Running with Docker
| |
Expected Output
Running operators.rb:
== Arithmetic ==
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a ** b = 1419857
10.0 / 3 = 3.3333333333333335
== Comparison ==
a == b -> false
a != b -> true
a > b -> true
a < b -> false
5 <=> 10 -> -1
== Logical ==
sunny && warm -> false
sunny || warm -> true
!sunny -> false
== Assignment ==
count after +=5, -=3, *=2 -> 24
name ||= "Anonymous" -> Anonymous
== String ==
concatenation -> Ruby Rocks
repetition -> ababab
interpolation -> Ruby has 4 letters
== Precedence ==
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
Running operators_methods.rb:
10
1
$7.50
nil
MATZ
Key Concepts
- Operators are methods —
2 + 3is really2.+(3). This is why you can define+,-,*,<=>, and others on your own classes to make them behave naturally. - Integer vs. float division —
17 / 5is3(truncated), but introducing a float like10.0 / 3produces a floating-point result. Mixing types promotes the result to a float. - The spaceship operator
<=>— returns-1,0, or1to indicate ordering. It powers sorting and comparison logic throughout Ruby’s standard library. - Short-circuit logical operators —
&&and||stop evaluating as soon as the result is known, returning the actual operand rather than a strict boolean. - Conditional assignment
||=— assigns a value only if the variable is currentlynilorfalse. It’s a common Ruby idiom for setting defaults. - Strings respond to
+and*—+concatenates and*repeats, but string interpolation (#{...}) is usually the most readable way to build strings. - Safe navigation
&.— calls a method only if the receiver isn’tnil, returningnilinstead of raisingNoMethodError. It’s invaluable when chaining method calls on values that might be missing. - Precedence follows math conventions —
*,/, and%bind tighter than+and-; use parentheses to make intent explicit when in doubt.
Comments
Loading comments...
Leave a Comment