1 00:00:00,890 --> 00:00:04,350 Now we're going to talk a little bit about using the extend method. 2 00:00:05,690 --> 00:00:08,780 We touched briefly before on class methods. 3 00:00:08,780 --> 00:00:13,060 Class methods are used when a method needs to have functionality, but 4 00:00:13,060 --> 00:00:16,090 it may not be tied to a specific instance of a class. 5 00:00:17,210 --> 00:00:20,580 It's much easier to understand if we actually look at some code to see how 6 00:00:20,580 --> 00:00:21,900 it works. 7 00:00:21,900 --> 00:00:24,615 Let's go ahead and see how that works now using WorkSpaces. 8 00:00:27,590 --> 00:00:32,260 Okay so I have a WorkSpace right here with another very simple class. 9 00:00:32,260 --> 00:00:34,500 And this is just a customer class. 10 00:00:34,500 --> 00:00:37,770 It has one attr_accessor for the name. 11 00:00:37,770 --> 00:00:43,090 We initialize the class with the name and set that instance variable. 12 00:00:43,090 --> 00:00:48,040 And then the to_s method just shows the name in brackets. 13 00:00:48,040 --> 00:00:52,530 So now, what we're going to do is create a module that will 14 00:00:52,530 --> 00:00:54,590 track different customers. 15 00:00:54,590 --> 00:00:59,500 And what I mean by that is, instead of keeping track of every single instance of 16 00:00:59,500 --> 00:01:05,120 a customer, we want to be able to call Customer.create with a name. 17 00:01:06,670 --> 00:01:11,300 And then that will be trackable on the customer class. 18 00:01:11,300 --> 00:01:13,380 So here's how we're going to do it. 19 00:01:13,380 --> 00:01:16,510 Let's go ahead and create a module called Tracking. 20 00:01:18,380 --> 00:01:23,380 Now, the methods that we're going to be writing in here are all 21 00:01:23,380 --> 00:01:26,300 going to be appearing like a mix in. 22 00:01:26,300 --> 00:01:30,300 However, since we're going to be using the extend key word, 23 00:01:30,300 --> 00:01:32,410 they will appear as class methods. 24 00:01:33,580 --> 00:01:35,510 So first, let's go ahead and 25 00:01:35,510 --> 00:01:39,650 create something which is a method called instances. 26 00:01:41,160 --> 00:01:45,970 And what this is gonna do is return an empty array 27 00:01:45,970 --> 00:01:48,450 the first time that we access it. 28 00:01:48,450 --> 00:01:53,030 Now this array, once we extend the class with this module, 29 00:01:53,030 --> 00:01:55,060 will be available on the class. 30 00:01:55,060 --> 00:01:59,080 Now that sounds a little bit strange, so let's just go ahead and see how it works. 31 00:01:59,080 --> 00:02:06,370 If we extend this with the Tracking module, this isn't going to work yet. 32 00:02:06,370 --> 00:02:09,720 Let's just see Customer.instances, and 33 00:02:09,720 --> 00:02:13,120 this should return an array, we'll just call inspect. 34 00:02:14,720 --> 00:02:16,200 And actually, let's go ahead and 35 00:02:16,200 --> 00:02:22,340 say Customer.instances and use a format string. 36 00:02:23,460 --> 00:02:25,720 And then put the output from that. 37 00:02:25,720 --> 00:02:31,310 So now if we run extend.rb, we can see we've got this empty array. 38 00:02:31,310 --> 00:02:35,980 Now this array exists only in the Customer class. 39 00:02:35,980 --> 00:02:38,890 And we get to it by accessing this method. 40 00:02:38,890 --> 00:02:42,210 So what this is going to do is conditionally assign this 41 00:02:42,210 --> 00:02:46,780 instances class variable at this point to an empty array. 42 00:02:46,780 --> 00:02:51,160 That will also return the array that we just created or 43 00:02:51,160 --> 00:02:54,860 it will just return the array if it's already been created. 44 00:02:56,250 --> 00:03:00,730 So now let's go ahead and implement this create method on Customer. 45 00:03:02,660 --> 00:03:05,860 And we're going to pass it one argument which is the name. 46 00:03:05,860 --> 00:03:10,390 Now we know any class that we mix in this tracking module into 47 00:03:10,390 --> 00:03:12,910 is going to have a name. 48 00:03:14,120 --> 00:03:21,740 So what we're gonna do is say object, is a new name. 49 00:03:21,740 --> 00:03:26,120 Now what this is going to do is call the initialize method 50 00:03:26,120 --> 00:03:28,870 of the class that this was mixed into. 51 00:03:30,090 --> 00:03:34,000 And return that and assign it to this object method. 52 00:03:35,200 --> 00:03:37,019 Then, we'll append that. 53 00:03:41,434 --> 00:03:47,050 To the instances array and return that variable. 54 00:03:49,340 --> 00:03:53,240 So the reason we call this object is because we don't know exactly 55 00:03:53,240 --> 00:03:58,050 what kind of object is going to come in to this tracking module. 56 00:03:58,050 --> 00:04:02,620 All we know is that we're passing the initialized method, the argument. 57 00:04:03,680 --> 00:04:05,970 We append it to the array, and then we return it. 58 00:04:05,970 --> 00:04:11,490 So now, let's go ahead and see how that works. 59 00:04:11,490 --> 00:04:16,606 So, we'll just say Customer.create, 60 00:04:16,606 --> 00:04:22,609 and then we will create a customer called Jason. 61 00:04:26,374 --> 00:04:28,369 And we'll do the same thing again with Kenneth. 62 00:04:31,174 --> 00:04:32,923 And then let's go ahead and look at the instances. 63 00:04:35,334 --> 00:04:38,120 Gonna clear my screen, and run this one more time. 64 00:04:38,120 --> 00:04:42,550 And you'll notice we've got no customer instances here, and 65 00:04:42,550 --> 00:04:46,570 then we properly create Jason and Kenneth. 66 00:04:46,570 --> 00:04:51,730 And now this instances array contains two different objects. 67 00:04:53,080 --> 00:04:57,880 So this isn't super useful, but let's say we wanted to find a certain customer. 68 00:04:57,880 --> 00:05:03,060 What we could do is create this method called find with the name. 69 00:05:04,680 --> 00:05:08,822 And since it's an array, we can just call instances.find. 70 00:05:15,926 --> 00:05:20,405 And return the first name that matches whatever it is we pass in to this 71 00:05:20,405 --> 00:05:21,340 find method. 72 00:05:23,150 --> 00:05:23,870 So let's see how that works. 73 00:05:26,537 --> 00:05:28,350 We'll go ahead and try and find Jason. 74 00:05:35,901 --> 00:05:37,340 Gonna clear my screen and run this again. 75 00:05:38,550 --> 00:05:40,360 And you'll see that we get back Jason. 76 00:05:42,420 --> 00:05:44,526 And lets go ahead and try and find a name that doesn't exist. 77 00:05:50,424 --> 00:05:53,950 And see what gets returned there, and nothing gets returned. 78 00:05:56,010 --> 00:06:00,530 So by calling extend and passing in the module name, 79 00:06:00,530 --> 00:06:04,286 instead of being mixed in to instances of the class, 80 00:06:04,286 --> 00:06:09,730 extend mixes in to the class methods of the class. 81 00:06:11,230 --> 00:06:13,300 It can be a little bit confusing. 82 00:06:13,300 --> 00:06:16,180 Go ahead and try it on your own now using WorkSpaces.