Variables4:48 with Jason Seifer
A class is composed of methods and variables. Variables can keep track of different states and values inside of a class. In this video, we'll modify our `Name` class to take a variable for the `title` attribute and see how that variable can be shared.
class Name def initialize(title) @title = title end def title @title end def first_name “Jason” end def middle_name “” end def last_name “Seifer” end end
[MUSIC] 0:00 Let's take the name class that we were just working with. 0:04 Currently looks like this. 0:07 We have the first, middle and 0:09 last name methods which return all of the different parts of my name. 0:11 This is effective if we're only ever going to use my name when writing our programs. 0:16 Instead, what we wanna do is make the name class more of a blueprint. 0:22 In order to do that, we're going to make those name and 0:27 title attributes variables within the class. 0:30 Let's go ahead and start with the title attribute, 0:35 which we're going to send in as an argument when we create our class. 0:37 Let's see how that works now, using work spaces. 0:42 So here's our class right now. 0:46 And, as we can see, it just returns my name and then prints it out. 0:48 So what we're going to do is make this a little bit more generic. 0:54 First, we'll start with this title method right here, 0:58 and we will have it print out a title that we send in, when creating the name. 1:02 Now, we can do that using an instance variable. 1:08 So we'll create this initialize method, and 1:13 initialize gets run when we instantiate an instance of this class. 1:16 We can also give the initialize method an argument, so let's do that with the title. 1:22 Now when we send that in, 1:31 we need to make this variable available to the rest of our class. 1:32 We do that by creating an instance variable. 1:37 An instance variable will be available to each method in the class. 1:40 We denote an instance variable by using the @ symbol. 1:46 And we will call this instance variable, title. 1:50 Now when this class gets instantiated, 1:55 we'll say it's equal to the title that's sent in. 1:58 Then, instead of returning a hard coded string, 2:04 we can return the instance variable, and that's what'll get printed out. 2:08 Now if we run this, as it is right now, we're going to get an error. 2:14 Because we don't send it in as an argument. 2:20 Let's go ahead and see what that looks like. 2:23 [SOUND] It says wrong number of arguments 0 for 1. 2:24 And it even tells us the line in which there's an error, 2:31 which is going to be line 2. 2:38 That's where the methods are being called. 2:41 And it will tell us where exactly the error was 2:44 encountered initially which is on line 23. 2:47 This is called a stack trace. 2:51 And it details where the error was encountered throughout the running of 2:56 the program, so now we can fix it based on the error message that we get. 3:01 So we know we need to send in a title. 3:06 [BLANK_AUDIO] 3:08 So we send the title in as a string. 3:10 Now if I run this again, this works just like it did before. 3:14 Now here's something that's neat about Ruby. 3:19 If we wanted to we could take all of these put statements out and 3:24 get this all on one line. 3:29 And just add a little plus to the end of the line. 3:33 Ruby will automatically know that the statement is continued on the next line. 3:38 So if we write it again the name is now printed out all on one line right here. 3:44 And we can even add spaces if we want to. 3:49 And we'll just run this again and make sure it works. 3:58 Okay, that printed out correctly. 4:00 So now what we can do is go through and we can set more instance variables here 4:05 [BLANK_AUDIO] 4:10 So, we set all these up to return the variable names, and 4:15 these are all methods that are being called. 4:18 And, all that they're returning is the actual variable name itself. 4:20 So now we need to set these as instance variables when the class is initialized. 4:26 [BLANK_AUDIO] 4:30 There we go. 4:39 Now let's go ahead and run that again and make sure it says the same thing. 4:40 And it does. 4:45 Perfect. 4:47
You need to sign up for Treehouse in order to download course files.Sign up