1 00:00:00,310 --> 00:00:03,860 Classes are useful for more than just storing attributes for 2 00:00:03,860 --> 00:00:05,520 the abstraction that we're working with. 3 00:00:06,590 --> 00:00:09,540 We can write methods inside of our classes. 4 00:00:10,550 --> 00:00:14,690 Writing methods inside of classes is powerful, because the methods can 5 00:00:14,690 --> 00:00:18,089 apply only to the individual instance that we're working with. 6 00:00:19,330 --> 00:00:23,980 Let's go ahead and take our Name class and add a method to it now using Workspaces. 7 00:00:25,760 --> 00:00:30,120 So, we've got our Name class and I just changed this to be an attr_accessor for 8 00:00:30,120 --> 00:00:32,240 a title, first name, middle and last name. 9 00:00:32,240 --> 00:00:35,819 So, we know that we're going to have all of those different methods set for us. 10 00:00:37,060 --> 00:00:42,860 Now, we can create our own methods to work with in classes. 11 00:00:42,860 --> 00:00:47,316 So, let's go ahead and create a method called full_name. 12 00:00:47,316 --> 00:00:54,006 [SOUND] And what this is going to do, 13 00:00:54,006 --> 00:00:59,082 is return the first_name, 14 00:00:59,082 --> 00:01:04,406 middle_name and last_name. 15 00:01:04,406 --> 00:01:06,449 And we'll put a space in between there too. 16 00:01:06,449 --> 00:01:16,422 [BLANK_AUDIO] 17 00:01:16,422 --> 00:01:23,185 Now instead of all of this different stuff right here we can call name.full_name. 18 00:01:23,185 --> 00:01:26,050 Let's go ahead and run that and see what happens. 19 00:01:28,870 --> 00:01:29,480 Okay. 20 00:01:29,480 --> 00:01:30,800 This prints out. 21 00:01:30,800 --> 00:01:32,030 Pretty much what we wanted. 22 00:01:32,030 --> 00:01:33,970 But let's put a space here. 23 00:01:33,970 --> 00:01:34,648 Okay. 24 00:01:34,648 --> 00:01:38,805 [BLANK_AUDIO] 25 00:01:38,805 --> 00:01:39,440 That looks good. 26 00:01:41,170 --> 00:01:42,060 There we go. 27 00:01:42,060 --> 00:01:44,160 The correct thing has been printed out. 28 00:01:46,490 --> 00:01:49,580 Now we can also define another method. 29 00:01:49,580 --> 00:01:50,729 We'll say full_name_with_title. 30 00:01:50,729 --> 00:01:56,484 [BLANK_AUDIO] 31 00:01:56,484 --> 00:02:01,517 And in this one we will return the title and the space and 32 00:02:01,517 --> 00:02:08,520 we can actually just call this method because it returns a string. 33 00:02:08,520 --> 00:02:11,720 This uses something called an implicit return. 34 00:02:11,720 --> 00:02:16,710 Which means that the last item on the line is going to be the return value for 35 00:02:16,710 --> 00:02:17,580 the method. 36 00:02:17,580 --> 00:02:19,090 In this case, it's a string. 37 00:02:20,190 --> 00:02:23,110 So we can use the results of that string later. 38 00:02:25,130 --> 00:02:26,890 And it would be just like calling the method. 39 00:02:28,720 --> 00:02:34,140 So now, we could write full_name_with_title. 40 00:02:35,580 --> 00:02:39,120 And if we run this again, we get exactly what we were expecting. 41 00:02:40,810 --> 00:02:46,810 Now the nice thing about using classes and 42 00:02:46,810 --> 00:02:52,641 objects [NOISE] is that the variables only 43 00:02:52,641 --> 00:02:57,625 apply to that specific instance. 44 00:02:57,625 --> 00:03:04,363 So if we created, let's say we call this one nick, 45 00:03:04,363 --> 00:03:13,500 this variable would only have access to its internal variables. 46 00:03:13,500 --> 00:03:16,140 They are local to this variable. 47 00:03:17,490 --> 00:03:22,130 So if we run that again, we can see that that gets printed out just like we expect, 48 00:03:22,130 --> 00:03:25,280 and this allows us to abstract that logic away. 49 00:03:25,280 --> 00:03:30,360 And these are both fully contained and have access only to their respective data.