1 00:00:00,685 --> 00:00:04,660 Enumerable is one of the most widely used modules in Ruby. 2 00:00:05,660 --> 00:00:09,740 We could do a whole course just on the Enumerable module, but 3 00:00:09,740 --> 00:00:11,890 we're only gonna take a quick look at it today. 4 00:00:13,470 --> 00:00:16,250 The Enumerable module is going to be very familiar. 5 00:00:17,270 --> 00:00:20,450 It gives us methods that we've worked with in arrays and 6 00:00:20,450 --> 00:00:24,040 hashes such as map, include, and find. 7 00:00:25,410 --> 00:00:29,150 Let's take a look at how Enumerable works now using Work Spaces. 8 00:00:30,700 --> 00:00:34,480 So, let's go ahead and quickly launch an irb console and 9 00:00:34,480 --> 00:00:37,210 just take a look at this hash. 10 00:00:37,210 --> 00:00:42,390 So, just gonna create a regular hash right here with two keys. 11 00:00:42,390 --> 00:00:49,150 And two values, saying my name is Jason and my location is Treehouse. 12 00:00:49,150 --> 00:00:53,660 Now, we've worked with hashes a little bit before during the Ruby curriculum, but 13 00:00:53,660 --> 00:00:55,410 let's just do a little refresher here. 14 00:00:55,410 --> 00:01:01,120 If we want to know if a hash has a certain key, we can use this include method. 15 00:01:01,120 --> 00:01:04,260 And then, pass it the key that we wanna see if it's there. 16 00:01:04,260 --> 00:01:10,250 So, if we look at name that's there, if we look at location that's there, 17 00:01:10,250 --> 00:01:14,170 if we look at age, that is not there. 18 00:01:15,690 --> 00:01:18,310 And then, we have a few more methods that we can work with. 19 00:01:18,310 --> 00:01:24,950 We could ask the hash if any of the items, keys or values. 20 00:01:24,950 --> 00:01:29,630 The value is equal to Jason and we get true. 21 00:01:30,670 --> 00:01:34,427 Or, we could ask the hash if all of the values, 22 00:01:38,967 --> 00:01:41,960 Are Jason, and we get false. 23 00:01:41,960 --> 00:01:46,820 And then we could even do something like selecting all of the different hash 24 00:01:46,820 --> 00:01:54,610 values, where the value's length is greater than five. 25 00:01:55,810 --> 00:01:58,260 And we only get that one hash key and value back. 26 00:01:59,610 --> 00:02:02,970 Now this a little bit of a refresher. 27 00:02:02,970 --> 00:02:08,480 If we look at the documentation for Ruby Hash we can see 28 00:02:08,480 --> 00:02:13,010 that some of these here, like each key each pair or 29 00:02:13,010 --> 00:02:18,040 even include, it returns true if the given key is present in the hash. 30 00:02:18,040 --> 00:02:23,760 All of these methods come from this Enumerable module, 31 00:02:23,760 --> 00:02:25,350 or the Enumerable mixin. 32 00:02:26,610 --> 00:02:31,290 Now what's really nice, is since Enumerable is a module, 33 00:02:31,290 --> 00:02:35,670 we can include Enumerable in our own classes. 34 00:02:35,670 --> 00:02:40,510 And then get access to all of those methods ourself. 35 00:02:41,560 --> 00:02:46,130 So all these methods like any, all, include, and 36 00:02:46,130 --> 00:02:50,700 find, find all, select, those are all part of Enumerable. 37 00:02:50,700 --> 00:02:53,190 Let's go ahead and see how that works. 38 00:02:53,190 --> 00:02:58,640 If we read the documentation, it says the class must provide a method called each, 39 00:02:58,640 --> 00:03:01,940 which yields successive members of the collection. 40 00:03:03,500 --> 00:03:06,740 So I'm going to go ahead and exit out of IRB here and clear the screen, 41 00:03:06,740 --> 00:03:10,740 and I'm going to open up this enumerable.rb example. 42 00:03:12,630 --> 00:03:16,200 Now if we look at this code this has two classes, 43 00:03:16,200 --> 00:03:20,030 a player and a game and these are really simple. 44 00:03:20,030 --> 00:03:24,000 The player class includes the comparable module and 45 00:03:24,000 --> 00:03:27,560 defines that spaceship operator with the player's score. 46 00:03:27,560 --> 00:03:29,180 And it has two attributes name and score. 47 00:03:30,200 --> 00:03:34,650 Now we also have this game class and the game class has players. 48 00:03:34,650 --> 00:03:39,700 It sets players to an empty array when the game is initialized and 49 00:03:39,700 --> 00:03:44,070 then we can use the add player method to push players onto that array. 50 00:03:45,230 --> 00:03:50,710 And then we can also get the score of this game, it just assembles the score of 51 00:03:50,710 --> 00:03:56,320 all the different players, so I've created a couple different 52 00:03:56,320 --> 00:04:01,310 game instances here and a few different player instances with different scores. 53 00:04:01,310 --> 00:04:03,030 Game1 has two players. 54 00:04:03,030 --> 00:04:04,640 Game2 also has two players. 55 00:04:04,640 --> 00:04:09,030 So we're just gonna run this and see what the game looks like. 56 00:04:09,030 --> 00:04:13,180 And we see we got this game instance and it's got an array of players. 57 00:04:14,480 --> 00:04:19,160 So now let's go ahead and implement the Enumerable module. 58 00:04:19,160 --> 00:04:24,210 And what we want to do is iterate through all of these game's players. 59 00:04:24,210 --> 00:04:25,300 So, we can say game1.any. 60 00:04:25,300 --> 00:04:30,150 We want to have access to this any method. 61 00:04:30,150 --> 00:04:32,105 We'll say player. 62 00:04:32,105 --> 00:04:36,490 Player.score is greater than 80. 63 00:04:36,490 --> 00:04:42,000 Now we know this should return true because Kenneth only has 80 points. 64 00:04:42,000 --> 00:04:43,520 We want greater than 80. 65 00:04:43,520 --> 00:04:45,830 But Jason has 100 points. 66 00:04:45,830 --> 00:04:46,660 Not trying to brag. 67 00:04:46,660 --> 00:04:51,360 Now if I were to run this right now we would see this fail out. 68 00:04:51,360 --> 00:04:54,400 And it says undefined method any for this game. 69 00:04:54,400 --> 00:04:58,370 And that's because we haven't included Enumerable yet. 70 00:04:59,530 --> 00:05:04,470 So if we do that, we include Enumerable, and we're gonna run this and 71 00:05:04,470 --> 00:05:05,670 it's going to fail again. 72 00:05:06,820 --> 00:05:12,980 Clear my screen there, and it says undefined method each for game. 73 00:05:14,930 --> 00:05:17,620 So let's go ahead and implement this each method. 74 00:05:17,620 --> 00:05:23,260 If we were to back to the Enumerable documentation, we can see the class must 75 00:05:23,260 --> 00:05:28,930 provide a method called each, which yield successive members of the collection. 76 00:05:30,440 --> 00:05:32,490 So we'll go ahead and define this each method. 77 00:05:36,070 --> 00:05:38,060 And we're gonna do something a little bit advanced here. 78 00:05:38,060 --> 00:05:41,960 We're gonna say that each method takes a block, and 79 00:05:41,960 --> 00:05:46,520 we're going to say that just goes out to players. 80 00:05:48,920 --> 00:05:53,230 Now, remember players is an array, so it has this each method. 81 00:05:53,230 --> 00:05:59,990 And all we are doing is passing this block as a variable to that each method. 82 00:05:59,990 --> 00:06:06,290 This is called pretzeling, and we're just passing around this block like a variable. 83 00:06:06,290 --> 00:06:07,320 So once we do that, 84 00:06:08,320 --> 00:06:11,950 this any method that we have at the bottom here should start working. 85 00:06:13,380 --> 00:06:14,740 And let's go ahead and print that out. 86 00:06:14,740 --> 00:06:22,650 And it looks like we get true, so one of the players does have a score above 80. 87 00:06:22,650 --> 00:06:27,918 Now since we defined this each method, we now have access 88 00:06:27,918 --> 00:06:33,750 to all of these different methods inside of Enumerable. 89 00:06:33,750 --> 00:06:36,960 And it makes it very very powerful. 90 00:06:36,960 --> 00:06:40,800 So we could do any, we could find all of the different players. 91 00:06:40,800 --> 00:06:42,590 So let's go ahead and do that. 92 00:06:45,060 --> 00:06:52,439 Let's go ahead and say players is game1.find_all player. 93 00:06:52,439 --> 00:06:56,345 Player.score is greater than 80. 94 00:06:56,345 --> 00:07:02,340 [SOUND] And then let's put there's with base score, 95 00:07:02,340 --> 00:07:08,888 greater than 80 and inspect that player's variable. 96 00:07:08,888 --> 00:07:14,040 Al right, and find all returns an array. 97 00:07:14,040 --> 00:07:19,870 If we just wanted to find the first one, we could change that to find. 98 00:07:19,870 --> 00:07:24,470 And then if we run that again, we can see it just returns one player instance and 99 00:07:24,470 --> 00:07:26,830 does not have an array. 100 00:07:26,830 --> 00:07:31,800 On your own, go ahead and check out this documentation and try playing around 101 00:07:31,800 --> 00:07:36,870 with a bunch of different methods inside of the Enumerable module. 102 00:07:36,870 --> 00:07:39,570 There's a link in the documentation and go ahead and 103 00:07:39,570 --> 00:07:44,580 try checking out Detect, Collect, Any and All, and 104 00:07:44,580 --> 00:07:49,180 all of those different methods, and give it a try in Work Spaces.