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.

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 .

📋 Quick Reference — Operators

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].