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
How can a dandelion hold back a flood? | Initiating the butterfly effect for good
I’m setting off on a journey around the world to follow the butterfly effect. I want to see how even a single action on one side of the globe can have a profound environmental impact on the other. My journey begins in Germany, with a family rewilding thei…
How can you tell when AI is being reliable?
One of the really weird things about large language models is they don’t work like we think computers should work. LLMs are very bad at math; computers should be good at math. Large language models are weirdly emotional and can threaten you or want to be …
3 Tips on Negotiations, with FBI Negotiator Chris Voss | Big Think
How you use your voice is really important, and it’s really driven by context more than anything else. Your tone of voice will immediately begin to impact somebody’s mood, and then at this immediately how their brain functions. There’s actually scientific…
Leon Botstein: Art Now (Aesthetics Across Music, Painting, Architecture, Movies, and More.)
Hi, my name is Leon Botstein. I’m president of Bard College and I’m a musician and a historian of music. My subject today is the arts and art and what is it and why should we study it. Why is it part of university curriculum? Why is it something that an u…
2015 AP Chemistry free response 2a (part 2/2) and b | Chemistry | Khan Academy
All right, now let’s tackle, in the last video we did the first part of Part A. Now let’s do the second part of Part A. So the second part of Part A, they say calculate the number of moles of ethine that would be produced if the dehydration reaction went…
Species and the environment | Mechanisms of evolution | High school biology | Khan Academy
So we tend to view evolution and natural selection and the formation of new species, which is often called speciation, as a slow process that could take tens or hundreds of thousands of years, or in many cases millions of years. And that’s why it’s always…