Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Something that you'll come across a lot when working with numbers is the need to compare them. Learn how to compare numbers to see if they're equal to each other or not equal, compare values, and more.

0:00
[? upbeat music ?] [Treehouse]

0:08
Now we're going to learn about the different comparison operators available to us when working with numbers.

0:16
Now we're going to talk about the different ways to compare numbers to each other in Ruby.

0:21
In order to do that I'm going to create 2 variables right now, A and B.

0:25
A is going to be equal to 10, and B is going to be equal to 20.

0:31
If we wanted to check whether or not these numbers were equal to each other,

0:36
we could use the double equal comparison operator.

0:40
If we say A equals equals B, this should return false,

0:46
and it does.

0:49
The next comparison operator is the does not equal operator.

0:54
This is represented using an explanation point and the equal sign.

0:58
This checks to see if the 2 variables or numbers do not equal each other.

1:05
In this case, it should return true,

1:08
and it does because 10 does not equal 20.

1:12
We also have the greater than and less than comparison operators.

1:17
These check to see whether or not the values are greater than or less than each other.

1:21
Let's see.

1:23
Is A greater than B?

1:26
No, it's not.

1:29
Let's see if it's less than B,

1:32
and it is.

1:34
In addition to just seeing if something is greater than or less than something else,

1:38
there are also comparison operators for greater than or equal to and less than or equal to.

1:44
Let's check that out now.

1:47
I'm going to create a third variable called C and set that to 10 as well.

1:54
Now let's check to see if A is less than or equal to C.

1:58
We should get true back,

2:02
and we do.

2:04
We can also see if B is less than or equal to C, and it is not.

2:10
Finally, there's the combined comparison operator.

2:14
This is sometimes referred to as the space ship operator.

2:18
This will return 0 if the first operand equals the second,

2:23
1 if it's greater than the second and 1 if it's less than.

2:29
Let's see how that works.

2:35
Here A is 10 and B is 20.

2:39
Therefore, we get 1 back because the right side is greater than the left side.

2:45
If we did this with the variable C we should get back 0,

2:50
and we do.

2:53
Finally, another comparison operator we have is a method called EQL with a question mark.

3:01
This is true if both items are the same type and value.

3:06
Let's see how that would work.

3:11
If we asked if A is equal to C, we should get back true

3:15
because A and C are both 10, and they're both fixed notes.

3:21
If we had a floating point number we should get back false,

3:30
and we do.

3:33
That can come in handy when you're writing Ruby programs.

3:35
[? upbeat music ?]

3:37
And those are the different comparison operators available to us when working with numbers.

3:44
[Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up