Operators & Expressions
Operators are the symbols — like + , == , and && — that combine values into expressions, and an expression is any piece of Ruby code that evaluates to a single value.
Learn Operators & Expressions in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end you'll do arithmetic, compare with the spaceship <=> , combine conditions with && / || , and swap variables with parallel assignment.
What You'll Learn in This Lesson
1️⃣ Arithmetic Operators
Ruby has the usual + - * / , plus % for the remainder (modulo) and ** for powers. The big gotcha: dividing two integers gives an integer, truncated toward zero. Mix in a .0 when you want decimals.
2️⃣ Comparison & the Spaceship
Comparison operators return true or false . The star of the show is <=> , the spaceship operator: it returns -1 , 0 , or 1 and powers all of Ruby's sorting.
3️⃣ Logical, Ternary & Ranges
In Ruby && and || return one of their operands, which makes value || default a tidy way to supply fallbacks. The ternary cond ? a : b is a compact if/else, and ranges built with .. and ... are first-class values you can convert, test, and iterate.
Your turn. Fill in each ___ blank with the right operator, then run it.
Mini-Challenge: FizzBuzz & a Range Fixer
Put modulo, logical operators, and parallel assignment together. Classify a number FizzBuzz-style, then swap two variables so the range comes out in order. Run with ruby ops.rb .
Common Errors (and the fix)
- ❌ 7 / 3 expecting 2.33 → ✅ integer division truncates; use 7.0 / 3 or 7.fdiv(3) .
- ❌ x = a and b behaving oddly → ✅ and has lower precedence than = ; use x = a && b .
- ❌ if x = 5 (assigns, always truthy) → ✅ compare with == : if x == 5 .
- ❌ Forgetting the ternary colon → ✅ the form is cond ? a : b , with spaces around the ? and : .
- ❌ (1...5) includes 5 → ✅ three dots exclude the end; (1...5) is 1,2,3,4.
Pro Tips
- 💡 Swap in one line: a, b = b, a needs no temporary variable.
- 💡 Defaults with ||: name = input || "Guest" falls back when input is nil/false.
- 💡 Operators are methods: 1 + 2 is really 1.+(2) , which is why your own classes can define + .
📋 Quick Reference — Operators
Concept
Ruby Syntax
Power / modulo
2 ** 10 , 7 % 3
Float division
7.0 / 3
Spaceship
a <=> b
Logical (tight)
&& , || , !
Ternary
cond ? a : b
Ranges
1..5 , 1...5
Swap / splat
a, b = b, a
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Arithmetic with + - * / % ** — integer division truncates
- ✅ Comparison and the spaceship <=> returning -1/0/1
- ✅ && / || return operands; and / or bind loosely
- ✅ Ternary cond ? a : b and ranges .. / ...
- ✅ Parallel assignment and the splat *rest
- ✅ Next lesson: nil, true, false & truthiness — what counts as "false"
Practice quiz
What does the spaceship operator return for (1 <=> 2)?
- 1
- 0
- -1
- nil
Answer: -1. <=> returns -1 when the left value is smaller than the right.
What does (3 <=> 2) return?
- -1
- 0
- 1
- nil
Answer: 1. <=> returns 1 when the left value is larger.
What does ("a" <=> 1) return?
- -1
- 0
- 1
- nil
Answer: nil. <=> returns nil when the two values cannot be compared.
What does 5 == 5.0 evaluate to?
- true
- false
- nil
- an error
Answer: true. == compares value across Integer and Float, so 5 == 5.0 is true.
What does 5.eql?(5.0) evaluate to?
- true
- false
- nil
- an error
Answer: false. eql? also requires the same type, and Integer 5 is not Float 5.0, so it's false.
What does (nil || "default") return?
- nil
- true
- "default"
- false
Answer: "default". || returns the first truthy operand; nil is falsy, so it returns "default".
What does (5 && 10) return?
- true
- 5
- 10
- false
Answer: 10. && returns the last operand when all are truthy, so it returns 10.
What does -7 % 3 evaluate to in Ruby?
- -1
- 1
- 2
- -2
Answer: 2. Ruby's % follows the sign of the divisor, so -7 % 3 is 2.
After a, b = 1, 2; a, b = b, a, what are a and b?
- 1 and 2
- 2 and 1
- 1 and 1
- 2 and 2
Answer: 2 and 1. Parallel assignment swaps without a temp variable, so a is 2 and b is 1.
After first, *rest = [10, 20, 30, 40], what is rest?
The splat *rest grabs the leftover items into an array: [20, 30, 40].
Continue this course
- Previous: Variables
- Next: nil, true, false & Truthiness