1 00:00:00,025 --> 00:00:07,621 [SOUND] Hi, I'm Jason, your Treehouse Ruby teacher. 2 00:00:07,621 --> 00:00:11,700 In this course we're going to be going over modules in Ruby. 3 00:00:11,700 --> 00:00:14,960 If you've been following along with other Treehouse Ruby courses 4 00:00:14,960 --> 00:00:19,500 you've already come across and used modules, although you may not know it. 5 00:00:19,500 --> 00:00:20,930 If you haven't, don't worry. 6 00:00:20,930 --> 00:00:24,140 We'll be doing a little bit of a refresher about classes and 7 00:00:24,140 --> 00:00:26,000 how they relate to modules. 8 00:00:26,000 --> 00:00:28,500 The two share some very special similarities. 9 00:00:29,600 --> 00:00:34,760 In Ruby, a module is kind of like a class or object in that it's its own thing. 10 00:00:35,790 --> 00:00:39,630 Modules are used for a few different purposes, containers, 11 00:00:39,630 --> 00:00:42,400 behaviors, and occasionally, storage. 12 00:00:43,750 --> 00:00:49,270 One of the most common uses of modules in Ruby, is adding behavior to classes. 13 00:00:49,270 --> 00:00:51,110 Let's take a game as an example. 14 00:00:52,160 --> 00:00:56,520 If we're building a game that had robots that could shoot laser guns, and 15 00:00:56,520 --> 00:01:00,550 as we're coding the game, we give the robots methods like LaserBlast, 16 00:01:00,550 --> 00:01:02,880 LaserDamage, DrawLaser and more. 17 00:01:03,980 --> 00:01:06,250 Later on, as the game design progresses, 18 00:01:06,250 --> 00:01:09,520 we decide that we have a few more kinds of characters. 19 00:01:09,520 --> 00:01:13,560 Like sharks that need to shoot lasers also because it's a crazy world out there, 20 00:01:13,560 --> 00:01:14,710 and they were jealous of the robots. 21 00:01:16,260 --> 00:01:20,680 We could take all of the methods from the robot class that have to deal with lasers 22 00:01:20,680 --> 00:01:22,420 and put them into a module. 23 00:01:23,480 --> 00:01:27,790 And we could call the module something like uses laser blaster, and 24 00:01:27,790 --> 00:01:30,030 that's where we could keep all of that behavior. 25 00:01:31,270 --> 00:01:34,840 If we were to include the, uses LaserBlaster module, 26 00:01:34,840 --> 00:01:39,750 in the shark class, it would now have access to all of those methods. 27 00:01:39,750 --> 00:01:43,370 When we do that, it's called using a module as a mix in, 28 00:01:43,370 --> 00:01:46,430 because we're literally mixing in behavior to a class.