Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Operators  ›  Lesson

Comparison Operators

Comparison 12 min== compares values, eql? also compares types, equal? checks whether two names point to the same object, and <=> returns -1, 0 or 1 to say which of two values comes first.

Comparison Operators

Bean Cart gives a loyalty stamp when an order is at least 5.00. The check total >= 5 looks trivial, but Ruby has several ideas of "equal", and one surprising operator, <=>, that powers every sort you will ever run.

The rule

== compares values, equal? compares identity, <=> answers minus one, zero or one.

The everyday operators all return true or false:

total = 5.0
total == 5      # => true   (equal value; 5 and 5.0 count as equal)
total != 4.5    # => true   (not equal)
total > 4.5     # => true
total >= 5      # => true
"tea" < "tub"   # => true   (strings compare letter by letter)

Strings compare character by character by their character codes, and every capital letter comes before every lowercase one, so "Zebra" < "apple" is true.

For stricter questions, Ruby offers methods:

1 == 1.0          # => true   same value
1.eql?(1.0)       # => false  same value AND same type? No
a = "latte"
b = "latte"
a == b            # => true   same content
a.equal?(b)       # => false  not the very same object
a.equal?(a)       # => true

The spaceship operator <=> compares two values and returns -1 if the left is smaller, 0 if they are equal, and 1 if the left is bigger. It returns nil when the two cannot be compared:

4.5 <=> 5.0     # => -1
5 <=> 5         # => 0
"mocha" <=> "latte"  # => 1
5 <=> "five"    # => nil

Sorting uses <=> behind the scenes. If you define <=> in your own class, Ruby can sort your objects.

There is also ===, the case-equality operator. It reads best as "does the right side belong to the left?": Integer === 5 is true and (1..10) === 7 is true. You rarely write it yourself, but case statements use it, as you will see later.

The shortcut

Pick the question, then the tool: "same value?" use ==. "same value and same type?" use eql?. "literally the same object?" use equal?. "which comes first?" use <=>.

Checking a Bean Cart loyalty stampStep 1 of 4
  1. The question

    Does the order total of 5.0 reach the stamp threshold of 5?

Drills

Answer fast — trust the rule.

quick drill

What does 5 == 5.0 return?

quick drill

What does 5.eql?(5.0) return?

quick drill

What does "mocha" <=> "latte" return?

quick drill

What does 3 <=> "three" return?

quick drill

With a = "tea" and b = "tea" on separate lines, which is false?

Remember

  • == compares values, equal? compares identity, eql? adds a type check.
  • <=> answers -1, 0 or 1 (or nil), and sorting depends on it.
  • Trap: capitals sort before lowercase, and 1 == 1.0 is true while 1.eql?(1.0) is false.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainKinds of equal

Explain the difference between ==, eql? and equal? using 1 and 1.0 and two separate "tea" strings, and explain what <=> returns and what uses it.