Ruby Tutorial 3 - Variables
Hey guys, this is mack101, and today I'm going to be teaching you about variables in Ruby.
So, a variable is essentially a way to store a value and access that value at a later time. A variable has two main components: it has a name, which is anything you decide, and it has exactly one value. That value can be a string, it can be a number, it can be a floating point, anything you want.
So, let me just go ahead and jump right in and show you how to use variables in Ruby. For those of you who have done algebra or any kind of math where variables are involved, this should be pretty familiar to you. So, let me go ahead and run irb, and I'm going to assign a value to a new variable.
In order to do an assignment, you type the variable name. I'll call it num. Then you type in equal sign, then you type a value. Now, I just so happened to decide to call my variable num; I could have called it whatever I want, as long as it starts with the lowercase letter. There are some letters or characters you can't put in a variable name, but for now, we're just going to stick to lowercase letters.
So, I decided to call my variable num, put an equal sign, and then I put the value. I'm just going to make it 10, which is an integer. Um, so doing this will essentially change the value of the num variable. Now, we have a variable that's just sitting in this big pool of memory called num, and inside num is the number 10.
So now we're storing 10 in that variable. To get the value that we're storing in a variable, all we have to know is the variable name. So, I know it's num, so I just type num, I hit enter, and it tells me the value that's being stored up in there. So, that's pretty straightforward, and this is essential because you can put this in an expression.
The same way we type 10 and it would return 10, if we type num, it returns 10. So therefore, we can do something like num plus 5, and that will be 15, just like 10 plus 5 would be 15. So, it's the same idea; you can substitute a variable anywhere where you would have an expression or value or anything like that.
So that is also something very important to understand. I can make another variable while still having this old one. I'll call this one number, and I'll make it 11. I can do number minus num, and it'll be 1 because we're doing 11 minus 10.
Okay, now I'm going to change the value of num. You can do this the same way you would do an assignment. Just say num equals, and then I'll make my new value negative 10. By the way, as you're seeing here, you can put spaces pretty much anywhere you want in Ruby. So, I prefer to put spaces before and after the equal sign; it just makes it look cleaner to me. But you certainly don't have to, it doesn't matter.
But there we go. So now, negative 10 is being stored in num. Now we can do something that you should be pretty familiar with, but maybe not with variables. We can type num.dot abs, and it returns 10. Now that makes sense because the abs method returns the positive version of the thing you call it on. But what some people are unclear about is the value of the num variable hasn't actually changed. So, let's type num; the value is still negative 10.
Okay, so the abs method doesn't actually change anything about the value. There are some methods that do, and we'll get to that in a little bit. But first, let's try this out with some strings. So, I'm going to make a new variable. I'll call it stir, and I'll make stir equal the quick brown fox. I put it in quotes, so we put "the quick brown fox" in quotes, and we're assigning it to the stir variable.
You should be probably expecting that when we type stir, it'll return the string that we assigned to it, just like normal. Now we could do something like this: "the quick brown fox" plus "jumps over the lazy dog," and it would return "the quick brown fox jumps over the lazy dog."
You can also substitute in the variable to do this so we can put stir in here: stir plus "jumps over lazy dog." Hit enter, and it returns "the quick brown fox jumps over the lazy dog." So that's just another more complicated example of how we could substitute a variable for an expression in a bigger expression.
So, hopefully, you guys can see why that works. You can picture, okay, where stir is, I'm just going to throw the value of stir, which is this; and then if that would work, then this will work, and it'll work the same way. But we can also call methods on the stir variable. For instance, we can do stir.dot capitalize, and it'll capitalize the string and return the capitalized string.
Um, and like I showed earlier, if we type stir, it'll still be lowercase, so that, you know, that's consistent with what we expected. Now there's one new thing that I want to introduce. Some methods, such as the capitalize method, have a version of them that will change the value.
So I can do stir.dot capitalize exclamation point, and the exclamation point is part of the method name because, you know, it's object.dot method name. So the exclamation point is part of that. Um, and now if we hit enter, it does the same thing that the regular capitalize method did, but now if we type stir, oh my god, it is capitalized.
So the exclamation point actually just changes that letter in the string. It doesn't actually change the value of the variable, but we won't get into that right now. For now, all you really need to know is, okay, dot capitalize won't actually change the value in the variable, and dot capitalize exclamation point will.
That's what usually, that's the convention: that exclamation point at the end of method names will change the object that they're on. They'll mutate the object, and without an exclamation point, they'll just create a new one and return that and leave the old one unchanged.
So, I think that pretty much sums up most of the stuff you need to understand about variables. You can call a method on them; you can change the value. I'll probably be doing more advanced stuff with this in a future tutorial, for instance, like switching the value of two variables or assigning multiple variables at once, but this is just the gist of that.
So, um, I hope you enjoyed this video. Thanks for watching, mad kids 101. Subscribe, and goodbye!