0
0
Rubyprogramming~5 mins

Comparison operators in Ruby

Choose your learning style9 modes available
Introduction

Comparison operators help you check if things are equal, bigger, or smaller. They let your program make decisions.

Checking if a user entered the right password
Comparing two numbers to find which is bigger
Seeing if a score reached a passing grade
Deciding if a date is before or after today
Syntax
Ruby
a == b  # equals
 a != b  # not equals
 a > b   # greater than
 a < b   # less than
 a >= b  # greater than or equal to
 a <= b  # less than or equal to

Use double equals (==) to check if two things are the same.

Use != to check if two things are different.

Examples
Checks if numbers are equal.
Ruby
5 == 5  # true
5 == 3  # false
Checks if strings are not equal.
Ruby
"cat" != "dog"  # true
"cat" != "cat"  # false
Checks if one number is bigger or smaller than another.
Ruby
10 > 7   # true
3 < 1    # false
Checks if one number is bigger or equal, or smaller or equal.
Ruby
4 >= 4  # true
2 <= 1  # false
Sample Program

This program prints true or false for different comparisons.

Ruby
puts 10 == 10
puts 5 != 3
puts 7 > 9
puts 4 <= 4
OutputSuccess
Important Notes

Remember, == checks value equality, not object identity.

Comparison operators return true or false.

You can use them in if statements to control your program.

Summary

Comparison operators compare two values.

They return true or false.

Use them to make decisions in your code.