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

Ruby Tutorial 3 - Variables


5m read
·Nov 3, 2024

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!

More Articles

View All
Pterosaurs 101 | National Geographic
(Dramatic music) - [Narrator] Much like today’s birds, pterosaurs ruled Earth’s Mesozoic skies. (Pterosaur cawing) Adapting to many different habitats while their dinosaur cousins roamed below. But these were no birds. Pterosaurs were flying reptiles, and…
Worked example: Logistic model word problem | Differential equations | AP Calculus BC | Khan Academy
The population ( p ) of ( t ) of bacteria in a petri dish satisfies the logistic differential equation. The rate of change of population with respect to time is equal to ( 2 ) times the population times the difference between ( 6 ) and the population divi…
Why I Don’t Feel Guilty for Busting Wildlife Traffickers | Nat Geo Live
(Onkuri speaks) Government agencies in many parts of the world either don’t know much about the problem of wildlife trafficking or they might be understaffed, they might be under-trained, they might be under-equipped. So, we go in to help them and supple…
Constant of proportionality from graph | 7th grade | Khan Academy
The following graph shows a proportional relationship. What is the constant of proportionality between y and x in the graph? Pause this video and see if you can figure that out. All right, now let’s do this together and let’s remind ourselves what a cons…
The next best thing to owning an airplane
If you fly under 150 hours a year, it just doesn’t make sense, not financially. You know, go get a fractional; that’s the next best thing to owning an airplane. Do you want to explain that a little bit? So, there are different ways to get an airplane. Yo…
Why This Is The Perfect Time To Start A Startup
Okay, so we’re going to be doing the light cone podcast on stage at Start School East. What do you guys want to talk about? I’m just kind of imagining what these Founders want. Like they traveled here on a bus for hours. The headline topic is why this is …