yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Ruby Tutorial 6 - Comparisons


4m read
·Nov 3, 2024

Hey guys, this is Mac Heads 101, and today I'm going to be teaching you how to make Ruby answer questions that a baby could answer.

What kind of questions are these? Well, they're very basic comparisons. For instance, is 2 greater than 1? Now, you obviously know that two is greater than one, but the challenge here is to make Ruby tell us whether two is greater than one or not.

I'm going to be showing you a couple of different operators that you can use in Ruby to compare two different values, similar to the way you would do it in math. Ruby will actually be able to tell you yes or no answers based on whether the expression is true or not.

So, let me head up to the IRB console and give you an example of this. Let’s say I have a variable a, which is 1, and a variable b, which is 2. I want to ask Ruby if a is greater than b. Now, obviously, a isn't greater than b because a is 1 and b is 2, and 1 isn't greater than 2, but I want Ruby to tell me that.

So, all I have to do to do this is type a > b and hit enter, and it says false because a is not greater than b. Now, okay, what if I want to ask it if b is greater than a? Well, I just do the same thing; I just flip b and a, just like you'd expect. It tells me true because 2 is, in fact, greater than 1.

Now, there are a couple of different questions I could ask Ruby using different operators. For instance, I can say, is a less than or equal to b? I can say a <= b. Now, I can also ask, is a greater than or equal to b? I can say a >= b, and it'll tell me false.

All right, now let's have a look at the two other operators: there's equal to and not equal to. Now, how you might express these as an operator isn't as obvious as the other ones. So, let's say I want to check if a is equal to b. I wouldn't say a = b because that will assign a to b or b to a, rather. Now, I don't want to do that; I want to compare them.

The comparison operator because of this dilemma is actually ==, so it's two equal signs next to each other, no spaces. So, I say a == b, I hit enter, and it says false because 1 is not equal to 2. Now, what if I want to ask, is a not equal to b? Now this is the opposite of the equal to operator; it will tell me true if they're not equal, and it'll tell me false if they are equal.

I can say a != b, and by !=, I just mean exclamation point equal sign. So, let's go ahead and see what this says. It says true because a is not equal to b.

These don't have to be variables either. I don't have to say a < b or a > b or stuff like that; I can actually just say 1 > 2, and it'll tell me false. I can say 20 < 30, and it'll tell me true. And, you know, I can say 20 <= 20, and it'll tell me true. I can say 20 != 20, and it'll tell me false.

So, I can do various comparisons, and I can even mix variables and numbers. I can say a < 20, and it'll tell me true, and I can say a < 0, and it'll tell me false. In fact, I can actually find the value of a just by using less than or equal to. I can say, is a equal to 3? No. Is a greater than 0? Yup. Is a less than 3? Yup. Is a equal to 2? Nope. Is a equal to 1? Yep. And, you know, I found the value basically using comparisons.

So, this is a very straightforward way to get Ruby to answer a yes or no question when you're comparing two numbers. You can also compare strings. For instance, I can say "alex" == "alex" and it'll tell me true. I can say "alex" == "alex1" and it'll tell me false.

I can also use not equal to, right? Like that. So, equal to and not equal to actually work for strings as well as numbers, and you'll find that this actually works. You know, I can say, is 1.5 > 2, and it'll tell me it is not. I can say, is it greater than or equal to 2? Nope. Is it less than or equal to 2? Yup.

So, I can compare floating points, integers, strings, anything like that, and I can get Ruby to answer in a yes or no fashion. Now, this might seem pretty redundant right now, and you're probably thinking, like, why on earth do I need Ruby to tell me any of this? I can see it with my own eyes, and the answer is it's not that useful when you're writing some code in the Ruby console, but it becomes very useful when you send someone a script.

You want the script to do some logic. So, let's say the user types their age, and they say they're under 16; the script might tell them one thing, and if their age is over 16, the script might tell them something else. In order to do that, the script needs to be able to ask Ruby, you know, is this value less than or greater than 16?

And, you know, you're not going to be around to say yes, it is. You know, Ruby has to make that decision. So, comparisons are very useful for logic, for control flow of your program. They're not so useful in the IRB console.

So I was just making this video to introduce you guys to the various comparison operators. In the next tutorial, I'm going to be showing you if statements using these comparisons, which will allow you to control the flow of your program based on the result of different comparisons.

But anyway, thanks for watching, subscribe, and goodbye.

More Articles

View All
Definite and indefinite articles | The parts of speech | Grammar | Khan Academy
So we’ve covered the basic idea that divides the usage of the from “a” and “an.” You know, “the” is the definite article, and “a” or “an” is the indefinite. So when you’re being non-specific in language, you would use the indefinite article, as in “May I …
Strategies for dividing multiples of 10, 100 and 1000
We’re going to do in this video is get some practice doing division with numbers that are multiples of 10, 100, 1000, things like that. So, let’s say we wanted to compute what 2400 divided by 30 is. Pause this video and see if you can calculate it using w…
Matched pairs experiment design | Study design | AP Statistics | Khan Academy
The last video, we constructed an experiment where we had a drug that we thought might help control people’s blood sugar. We looked for something that we could measure as an indicator of whether blood sugar is being controlled, and hemoglobin A1c is actua…
Surface area to volume ratio of cells | Cell structure and function | AP Biology | Khan Academy
So let’s say that this is a cell. We know that all sorts of activity is going on inside of this cell here, and we will study that in a lot more depth as we go further in our study of biology. But it’s important to realize that this cell and the activity …
Unleashing the Power of the Mind Through Neuralink #Shorts
Each near-link N1 chip is roughly 4x4 millimeters with a thousand electrodes each. It’s feasible to fit up to 10 of these inside your head in different areas, all to measure and affect different parts of your brain. Using just 256 electrodes, or about two…
Give Society What It Doesn't Know How to Get
You’re not going to get rich renting out your time, but you say that you will get rich by giving society what it wants but does not yet know how to get at scale. That’s right. So essentially, I could… We talked about before, money is IOU’s from society sa…