Join Kevin Skoglund for an in-depth discussion in this video Variables, part of Ruby Essential Training.
In this movie, we're going to talk about variables in Ruby and I'm sure that you're going to think that I've already lied to you because I told you that everything in Ruby is an object. But here I am. I'm going to talk about variables and variables are not objects. They are a rare exception. It's kind of strange that we're going to start out talking about it, but we're going to use variables a lot, so we need to go ahead and cover it. Variables are just part of the Ruby language. Variables are going to be used to keep track of our objects and to give us an easy way to talk about those objects and reference them while we are programming. We can treat variables just like objects because once a verbal is assigned an object as a reference, it acts just like that object.
So, a variable always either be undefined or will act like an object. So, variables will seem like objects even though they're not. Variables work pretty much like the variables that you had back in algebra class, except that we need to assign a value to them before we can begin using them. Let's take a look. I'm going to open up an IRB session and let's go ahead and use our first variable. We will just call it x, just like we had in algebra. But like I said, we want to assign a value to it. Let's try x+2, just to see what that gives us. x is undefined, it doesn't know what to do with it.
We need to assign a value for it. So x=1. Now we can use our up arrow and go back to x+2 and now x has a value. So that's how variables are going to work. It just stores the object 1. 1 is an object. We'll talk little more about that in the next movie. But for now, just notice how x is a variable that is pointing to an object. Now, 2 is also an object, and 3 that gets returned by x+2? That's also an object. In fact remember when we said puts x+2 that nil that we got back is also an object.
So, really and truly, everything is going to be an object in Ruby except for these variables which are just used to allow us to point to objects. Now, all programming language have a variable naming convention. A lot of variables have a start with the dollar sign in a lot of languages. In Ruby that's not true. All we have to do is have a all lower case, underscored name. So first variables equals 3, okay. That's a well named variable in Ruby. Now, we don't want to put mixed case. All right. You don't want to do something like this, like you do in a lot of languages.
We don't want to put dashes in there. It needs to be an underscore and typically you would not run words together like that. We would go ahead and make it more readable like the English language by just putting an underscore in there, and that way it's nice and legible. We can quickly read our code and know what it's doing. So in the same way since Ruby is readable, we want to try for readability too. For example, we don't want to do aw_counter = 100. We want it to say articles_written = 100. So it's very clear right away what we're talking about. We don't have to sort of decrypt the name of our variable.
So, give your variables good common sense names. Now lot of times in these tutorials I'm going to be trying to show you something quickly, so I'm going to use something like x or maybe I might just say var=1. That's fine because we're just doing this for a demonstration purpose. If I were writing an actual program, I would want something that gave me a little bit more of a clue as to what was happening in the program. Now, as I said, variables are references that can be assigned. So for example, a=100 and then we could have b=a. Now, if we ask for the result of b, which we can do just by typing a variable on a line, it will return the value that variable, the object that it points to.
It's equal to 100 also. All right, it's not equal to a, because a is just a pointer to an object. So, b takes on that same reference and in fact if we do a = 50 now and we asked for b again, be is still pointing at 100. If you programmed in other languages before, this probably makes a lot of sense to you. There is one additional aspect of variable naming that I want to touch on here, even though we're going into a lot greater depth about it later on and that is the variable scope indicators. In addition to just having a lower case underscored word be the name of our variable, we can also put some additional characters in front of it that will let Ruby know what the scope or the variable ought to be.
In other words where it should be available? Scope determines whether or not we have access to these variables from inside classes, methods, and other coded structures and we'll talk more about it once we introduce those structures, but it's kind of hard to show you examples when right now we only have our global scope to look at. So what we've been using so far are local variables, ones wherewe have nothing in front of it, and all you really need to know for now is that variable names can began with the $ sign or the @ the front of it in order to give it different scopes. So, $ sign in front of it would give it global scope.
Putting two @ signs in front of it makes it a class variable. Putting one @ sign in front of it, it becomes an instance variable and we'll be working with these later on. For now just know that these are all legitimate ways that you can name your variable in Ruby.
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish