1 00:00:00,350 --> 00:00:03,800 When we create different variables inside of a class, 2 00:00:03,800 --> 00:00:06,970 not every method can see the variables that we use. 3 00:00:08,018 --> 00:00:12,610 If we wanna make a variable visible to different methods inside of a class, 4 00:00:12,610 --> 00:00:15,270 we call it an instance variable. 5 00:00:15,270 --> 00:00:18,280 That means that the variable should be visible to 6 00:00:18,280 --> 00:00:21,720 the whole instance of the class. 7 00:00:21,720 --> 00:00:25,470 If we want a variable that's only visible inside of the method or 8 00:00:25,470 --> 00:00:29,910 block that we're working with, that's called a local variable, and 9 00:00:29,910 --> 00:00:33,350 this whole concept is called the scope of a variable. 10 00:00:34,370 --> 00:00:36,979 Let's see how that works now using Workspaces. 11 00:00:38,450 --> 00:00:40,040 So, here's our Name class, and 12 00:00:40,040 --> 00:00:46,710 let's talk a little bit about local versus instance variables. 13 00:00:46,710 --> 00:00:51,750 So, here we are in the full_name method, and we also have full_name_with_title. 14 00:00:51,750 --> 00:00:53,400 Let's go ahead and 15 00:00:53,400 --> 00:00:59,970 pretend that we were creating a couple different variables inside of this method. 16 00:00:59,970 --> 00:01:03,240 So, let's say that we wanted to assemble the first and middle name. 17 00:01:05,120 --> 00:01:10,140 We could create a variable that says first _and_middle_name. 18 00:01:10,140 --> 00:01:14,875 We generally wouldn't do something like this, but just follow along for a second. 19 00:01:14,875 --> 00:01:21,115 [BLANK_AUDIO] 20 00:01:21,115 --> 00:01:26,650 We now have access to this first _and_middle_name variable right here. 21 00:01:26,650 --> 00:01:32,090 So, what we could do is, I'm gonna comment this out, and 22 00:01:32,090 --> 00:01:40,415 we could say first_and_middle_name plus space plus @last_name. 23 00:01:42,330 --> 00:01:46,520 And that should still return the correct item. 24 00:01:46,520 --> 00:01:50,950 Now let's go ahead, get this out of here for now. 25 00:01:50,950 --> 00:01:56,220 So let's run this by typing ruby name.rb. 26 00:01:56,220 --> 00:01:59,640 And it's printing the same thing, which is what we wanted. 27 00:01:59,640 --> 00:02:05,586 But we do not have access to this first_and_middle_name variable 28 00:02:05,586 --> 00:02:08,770 outside of this full_name method. 29 00:02:09,820 --> 00:02:12,575 The only way that we would have access to it 30 00:02:12,575 --> 00:02:15,503 would be if we made it an instance variable. 31 00:02:15,503 --> 00:02:17,860 And we can see this if we use irb. 32 00:02:20,080 --> 00:02:25,980 And we can do a shortcut to what I showed you earlier where you type in load and 33 00:02:25,980 --> 00:02:31,510 the path, and by typing irb -r name.rb. 34 00:02:31,510 --> 00:02:33,354 Oops, looks like I forgot the path. 35 00:02:33,354 --> 00:02:37,791 [BLANK_AUDIO] 36 00:02:37,791 --> 00:02:39,050 Okay. 37 00:02:39,050 --> 00:02:43,664 Now let me go ahead and create this name variable. 38 00:02:43,664 --> 00:02:54,241 [BLANK_AUDIO] 39 00:02:54,241 --> 00:02:59,380 Now we can see printing out full_name works when we call the method. 40 00:02:59,380 --> 00:03:02,231 But if we were trying to call first_and_middle_name, 41 00:03:02,231 --> 00:03:03,284 this would not work. 42 00:03:03,284 --> 00:03:06,854 [BLANK_AUDIO] 43 00:03:06,854 --> 00:03:10,724 This doesn't work because it is just a local variable. 44 00:03:10,724 --> 00:03:13,595 [BLANK_AUDIO] 45 00:03:13,595 --> 00:03:20,580 If we wanted to change it to an instance variable, we would then have access to it. 46 00:03:22,660 --> 00:03:24,250 Now, since we changed this file, 47 00:03:24,250 --> 00:03:27,360 we're going to have to reload it into our irb session. 48 00:03:30,250 --> 00:03:32,047 Now let me go ahead and create a name again. 49 00:03:32,047 --> 00:03:42,047 [BLANK_AUDIO] 50 00:03:44,792 --> 00:03:47,750 Now when we call first_and_middle_name, 51 00:03:47,750 --> 00:03:51,098 we're getting this undefined method again still. 52 00:03:51,098 --> 00:03:55,703 And the reason is we haven't written an attribute reader for it. 53 00:03:55,703 --> 00:03:56,870 So let's go ahead and do that. 54 00:03:56,870 --> 00:04:04,179 [BLANK_AUDIO] 55 00:04:04,179 --> 00:04:05,312 And we'll reload this. 56 00:04:05,312 --> 00:04:15,312 [BLANK_AUDIO] 57 00:04:20,894 --> 00:04:23,397 And we can see that that's nil. 58 00:04:23,397 --> 00:04:32,306 If we call full_name, it will have been set when we called the full_name method. 59 00:04:32,306 --> 00:04:37,578 And now we can see first_and_middle_name is correct. 60 00:04:37,578 --> 00:04:43,134 So, visibility of variables is something very important to keep in mind, 61 00:04:43,134 --> 00:04:47,640 and this is referred to as the scope of a variable. 62 00:04:47,640 --> 00:04:55,034 The scope, meaning where we are in the method and what's visible variable-wise.