Variables in Ruby

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 292,691 students

Lecture description

  • Explore variables, which serve as names or references to objects in Ruby

  • Practice assigning values to variables

Learn more from the full course

Learn to Code with Ruby

A comprehensive introduction to coding with the Ruby programming language. Complete beginners welcome!

31:25:46 of on-demand video • Updated September 2020

  • Learn to program in the Ruby programming language
  • Master common Ruby objects like Strings, Arrays, Hashes, and more
  • Solve popular coding challenges and algorithms with Ruby
  • Dive into object-oriented programming including classes and more
English [Auto] Are right in this lesson we'll begin our exploration of variables. What is a variable. A variable is a reference to an object. In other word that is commonly used for it is a placeholder as well as a pointer or an identifier. So as we've discussed previously almost everything in Ruby is an object and an object is just a thing or a data structure within a programming language. So a string is an example of an object. A number is an example of an object in Ruby. A floating point number with a decimal component is an example of an object in Ruby and a variable is basically just a placeholder name that we can give to a certain object such as a string so that instead of having to write it out each and every time we can simply use something that acts as a reference to that object or a pointer to that object it's important to iterate now that the variable itself is not an object. It's simply a placeholder It's an alternate name that we can use to refer to an object such as a number or a string. Ruby is dynamically typed if you're coming into this course without any previous experience in programming that kind of sounds like something bizarre. But in other programming languages whenever you use variables and declare variables you have to specify what data type you're using. So whenever you create a variable you have to say this pointer is going to point to a number like an integer or a string in Ruby that does not have to happen it is not required. So variable types do not have to be declared. You simply write the name that you'd like to give to your variable and then you point it or assign it to whatever it's going to be representing or serving as a placeholder for it. So the same variable in Ruby can be assigned to objects of different data types so the same variable can point to a integer or a number and then they can later point to a string in the exact same program the exact same variable. We'll see how this works in the code editor and just a second. So I'm going to show you some syntax for creating a variable we can see it appear right here on the box on the top right. These are just four separate variable declarations and we can see here that the syntax is fairly similar for all of them. We begin with the variable name or the placeholder name that we'd like to create. Here I have four of them name last name handsome and age. I followed those variable names with an equal sign and on the equal sign I write the object that I'd like to point that placeholder or that variable too. So in the first example Borris which is a string is what the name variable is pointing to in the second example my last name passed caver as a string is what the last name variable is pointing to. The third example introduces something new which is a Boolean will dive into that in much greater detail in future lessons but a boolean is basically a value that's equal to either true or false. It's not the same thing as a string. You can see that true there is not written in double quotes but you can see that the same variable logic applies we have handsom on the left side of the equal sign. That's our variable name and on the right side of the equal sign we have the object that that variable is pointing to. And on the bottom line we have the exact same thing we have a variable called age and it's pointing to an expression on the right side of the equal sign. Twenty plus 1. So variables are pointed to objects with the equal sign. The right side of the equal sign is always evaluated first. The only real example here that demonstrates this is the very last one you can see we have age equal to 24 plus one. What Ruby is going to do is evaluate 24 plus one it's going to give 25 as the final result. And that object that number of 25 is what age the variable is going to point to or reference to. So whatever happens on the right side is always going to happen first that evaluation completes entirely before anything is assigned to a variable. Similarly if we had some kind of concatenation operation if we had something like two strings being combined with the plus operator the plus sign those two strings would first speak concatenated and then that final result that new String object would be what that variable on the left side of the equal sign would point to. Variable names should start with a lower case letter or an underscore or you can see in the examples above. All of my variables start with lower case letters. That's a best practice. In addition spaces are not allowed. That breaks up the variable name. So whenever you have to use spaces such as in the last name you use underscores Instead we can see here between last name of place and underscore and this type of typing reformatting is called Lower Snake case. What that means is that everything is lowercase and words are separated by underscores. The reason it's called Snake case is because whenever you have a bunch of words and they're all separated by underscores. It sort of resembles a snake. So that's where that name comes from. But this format that we see right here with last name is called Lower Snake case and you should follow it whenever you are writing variable names separate each word each lowercase square with a underscore variable names are case sensitive. What that means is that if I have a variable with a capital H here if I had a name with an capital A M E and I pointed to something different that would be a completely separate variable in a completely separate reference. So you have to be careful because if you use variables and one of them has a different casing and one of the letters that another one does that's going to run into all kinds of errors in your code. So the general recommendation is to not ever have any variable names repeat with different K-6 just follow a consistent casing which is all lowercase and don't reuse the same variables over and over again with different cases. That's just begging for errors in your program. So now the last thing that I want to show you before we dive immediately into the code editor is just to not use any of Ruby's reserved keywords in any of your variable names. So so far we've really only used a couple keywords or methods in Ruby we've used put serve prent and P basically don't use those as variable names. We can see in the example above we use Name Last Name handsom age I didn't name any variable print or ports or any of the other 40 plus Ruby key words that exist in the language. Again it just makes it confusing for the language to figure out what you want to do if printed both means a command to output text and also a variable name that's pointing to some kind of string so don't ever use any of Ruby's reserved keywords in your variable names. Of course having a variable name like print underscore something is totally OK because it's different but don't use any literal Ruby key words like literally puts or literally print as your variable name to avoid any errors. So now let's dive into the ruby editor in atom and take a look at variables in action. All right so in our in our Adam text editor and let's basically replicate what I just wrote in my slide presentation. So I'm going to write name which is going to represent my variable. I'm calling it name that's entirely my choice I can choose whatever variable name I want as long as it begins with a a letter or an underscore it is all lowercase and this could be name. This could be first name that's b blah blah name is just my name for a variable and on the right side of the equals sign. I write the object that I want that variable to refer to. So I can write something like Boris that's a string that's a valid string. And let's try another example below. I'm going to create a variable name called that last name and that's going to refer to my last name. Past caver you can of course fill these strings in with your own first name and last name. These are just random strings that I've created random string objects that I'm assigning these variables to are pointing these variables too. And let's do another thing let's do handsom which is again just a variable name. This could be beautiful or charming whatever you want I'm just calling it handsome. I'm going to do the equal sign. That is the common syntax for appointing a variable to an object. And on the right side of the equal sign I simply write what I want that variable or placeholder to point to. Again this is called a boolean. We'll dive into it in more detail later. For now you can simply write true and below. I'm going to write age which is going to be another variable name again. All lower lowercase equals two pointed to something and I'm going to point it to an expression or an operation which is going to be 24 plus 1. So again this is going to be calculated to 25 and then Ruby is going to take age and pointed to 25. So what is the advantage of variables. Well variable store information they store or point to objects and we can use that. So we don't have to recall exactly what something is referencing for example if I forget my name but I remember that it's stored in a name I could write puts And instead of writing it out as a string I can simply write puts name and where Ruby is going to do is look up a name. It's going to find a declared above right here and it's going to substitute it with whatever string I've pointed name to. So when I execute this you'll see that Boris is going to put output right here. Similarly if I do something like puts a last name we can see that this string that last name is pointing to is now going to appear in my abdomen and output to the rank and operations are equally doable. So for example if I wanted to combine those two strings what I can do is do puts name plus a last name. This is literally the exact same thing as writing puts for us plus passkey. But again variables allow it allow us to have a little bit of an easier time because we can simply write the variable name and they can reference something like a much longer string or a much longer mathematical operation and just make it easier for us. I don't have to type it out each time but this is going to basically give us the exact same results. We can see it outputted both of these lines on the right here. I'm just going to remove this one and when we can even do here is just add an additional string in between. So variables don't have to necessarily just be concatenated in this case with other variables. We can do something like name plus a string with a space plus. And then the last name variable and what that's going to do is output for us is going to concatenate and space to the end of that. And then it's going to concatenate the string in last name. It's going to get that final result and that's what's going to be output to my atom runner view right here. Let's take a look at another example let's output the value in the handsom variables. I'm going to do puts handsome and that's going to give me true and I can sum the early output age and what this is going to do is allow us to see that age is in fact equal to 25. So Ruby has taken this expression evaluated and given me a new number that's 25. And because this is a number what we can actually do is do mathematical operations a lesson and go we explored how to do mathematical operations so we can do something like. Puts age plus five. It may seem kind of walk at first because you're seeing this word plus five and certainly if we did something like a string like puts age plus five. Let me just comment this line of code. For now we will get an error because this time we're trying to add the number five to a string with the characters age. But this is different this age right here does not represent a string age is simply the variable name that's storing the result of this operation will just 24 plus one. So Ruby is going to substitute this placeholder age with the number 25 which we got from right here. It's then going to add 5 to it to get 30. And we're going to get 30 output right here so we can do something like age plus five. And that's one way to perform that operation. Age again is just a variable name it's a word to us but to the computer it's just a placeholder that points to something. In this case it's pointing to an operation of 24 plus one. As I mentioned in the slide deck Ruby has dynamic typing which means that variables can be reassigned and they can be reassigned to different types of objects. Now of course variables can be reassigned to the same types of objects. For example if I output just age by itself we can see that age is currently equal to 25. What I can do is set age equal to something different. I can do. Age equals 50. And this assignment on line 15 is going to overwrite what was the assignment on line number four. So when I output age immediately below 8 is now going to equal 15. So variables are called variables because they vary because they can change their values so we can see that age here went from being 25 to being 50. What makes Ruby unique is that the datatype that a variable points to can change as well. So for example if I have a variable called favorite number and I'm going to point to a number object which is 24 we can see if I output favorite number there it is right here 24. What I can actually do is reassign favorite number to a totally different object type. I can reassign it to a string like 24. And now when I output favorite number we can see here in the output that 24 has appeared we can see it's a string. I've used that method and those double quotes have appeared. And this is a totally different object type compared to the number above which is 24. So variables can be pointed re-appointed to objects of different types. That basically is what dynamic typing means. And Ruby is a dynamically typed language. So that is a good introduction to variables will certainly be diving into this in much greater detail. So I'll see you in the next lesson.