1 00:00:03,498 --> 00:00:04,579 [SOUND]. 2 00:00:04,579 --> 00:00:08,050 We've learned how to write the simplest possible module. 3 00:00:08,050 --> 00:00:11,070 However, it really didn't do much of anything, 4 00:00:11,070 --> 00:00:15,000 because we were only talking about the namespacing aspect of modules. 5 00:00:16,160 --> 00:00:20,070 Next we're going to take a look at creating a mix-in. 6 00:00:20,070 --> 00:00:24,470 If we want to mix in behavior to a class, we have to use the include method. 7 00:00:25,710 --> 00:00:30,270 Unsurprisingly, based on the name, that will include all of the methods 8 00:00:30,270 --> 00:00:32,650 in the module in the class that we're working with. 9 00:00:34,030 --> 00:00:37,050 In order to make our modules actually do something, 10 00:00:37,050 --> 00:00:40,040 it needs to be included in a class. 11 00:00:40,040 --> 00:00:42,495 Let's see how that works now using workspaces. 12 00:00:45,010 --> 00:00:46,956 Okay, so let's go ahead and 13 00:00:46,956 --> 00:00:51,671 create our very first module that does something when we include it. 14 00:00:51,671 --> 00:00:56,811 So we're gonna call this fetcher.rb. 15 00:00:56,811 --> 00:01:01,770 And let's go ahead and create a class for a dog. 16 00:01:03,080 --> 00:01:06,301 And the dog is going to have a name. 17 00:01:11,181 --> 00:01:13,950 So we'll go ahead and create a new dog instance here. 18 00:01:15,400 --> 00:01:16,620 Here is Fido. 19 00:01:17,830 --> 00:01:23,680 Okay now we're gonna make a module, that we can include in the dog class, 20 00:01:23,680 --> 00:01:27,010 in case we wanted a dog to be able to fetch. 21 00:01:27,010 --> 00:01:30,910 So we'll call this module fetcher. 22 00:01:32,800 --> 00:01:35,760 And now we'll create a module called fetch, 23 00:01:36,850 --> 00:01:41,410 which is going to take one argument, which is the item that is being fetched. 24 00:01:42,750 --> 00:01:49,951 And then whatever is happening here we'll say, I'll bring that item right back. 25 00:01:49,951 --> 00:01:58,351 What we have to do is include the fetcher module in the Dog class. 26 00:01:58,351 --> 00:02:02,903 So, once we have this module here included in the Dog class, 27 00:02:02,903 --> 00:02:08,349 we can go back up here and any of the methods that are in this fetcher module 28 00:02:08,349 --> 00:02:14,390 will be included or mixed in to the Dog class and we have access to them. 29 00:02:14,390 --> 00:02:18,010 So we could now say something like dog.fetch, and 30 00:02:18,010 --> 00:02:20,900 then passing an item which is going to be ball. 31 00:02:22,700 --> 00:02:24,630 So if we run this, let's go ahead and see what happens. 32 00:02:26,220 --> 00:02:29,640 Dog says I'll bring that ball right back. 33 00:02:29,640 --> 00:02:30,740 Now the benefit for 34 00:02:30,740 --> 00:02:35,670 something like this is if we had another class, let's say, called Cat. 35 00:02:38,530 --> 00:02:40,960 We could say cat is Cat.new. 36 00:02:40,960 --> 00:02:43,780 What's a good name for a cat? 37 00:02:43,780 --> 00:02:46,470 So we'll create a new cat called Garfield, and 38 00:02:46,470 --> 00:02:52,560 now we can say cat.fetch laser pointer. 39 00:02:52,560 --> 00:02:56,610 So if we run that, because the Cat class includes the fetcher module, 40 00:02:56,610 --> 00:02:58,860 we get all of the methods inside of there. 41 00:03:00,650 --> 00:03:03,470 And if we run it again we can see that the cat says it's going to 42 00:03:03,470 --> 00:03:05,380 bring the laser pointer right back.