Comparison Methods

You’re getting good at this, so I’ll try to let the code do the talking. First, to see whether one object is greater than or less than another, we use the methods > and <:

puts 1 > 2
puts 1 < 2
false
true

No problem.

Likewise, we can find out whether an object is greater than or equal to another (or less than or equal to) with the methods >= and <=:

puts 5 >= 5
puts 5 <= 4
true
false

And finally, we can see whether two objects are equal using == (which means “Are these equal?”) and != (which means “Are these different?”). It’s important not to confuse = with ==. = is for telling a variable to point at an object (assignment), and == is for asking the question “Are these two objects equal?”

puts 1 == 1
puts 2 != 1
true
true

Of course, ...

Get Learn to Program, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.