1 00:00:00,000 --> 00:00:04,950 [MUSIC] 2 00:00:04,950 --> 00:00:05,500 Hi. 3 00:00:05,500 --> 00:00:08,710 I'm Jason, your Ruby teacher here, at Treehouse. 4 00:00:08,710 --> 00:00:13,240 In this workshop, we're going to be going over the enumerable module. 5 00:00:13,240 --> 00:00:17,740 The enumerable module is a mix-in that can be used in our Ruby classes that 6 00:00:17,740 --> 00:00:20,760 lets us get a lot of behavior for free. 7 00:00:20,760 --> 00:00:24,980 We're gonna go ahead and see how a lot of those methods work now in this workshop. 8 00:00:24,980 --> 00:00:26,350 So let's go ahead and get started. 9 00:00:28,020 --> 00:00:32,120 Okay so now we're going to take a look at the Enumerable module, 10 00:00:32,120 --> 00:00:36,550 and we're gonna take a look at it in a little bit more depth and 11 00:00:36,550 --> 00:00:44,350 view some demonstrations about how to use some of these methods in our classes here. 12 00:00:44,350 --> 00:00:45,750 So let me go ahead and launch irb. 13 00:00:45,750 --> 00:00:50,290 So I'm gonna create some variables to work with here. 14 00:00:50,290 --> 00:00:52,130 First one is array. 15 00:00:52,130 --> 00:00:53,865 It's gonna have some elements in it. 16 00:00:57,598 --> 00:00:58,474 And a hash. 17 00:01:07,368 --> 00:01:09,180 Okay. So we've got this array and 18 00:01:09,180 --> 00:01:11,150 this hash right here. 19 00:01:11,150 --> 00:01:14,420 So the first method we're gonna take a look at is the each method. 20 00:01:14,420 --> 00:01:19,478 So, with an array, which includes the enumerable module, each looks like this. 21 00:01:19,478 --> 00:01:24,060 Array.each {|item|, and we'll just print it out. 22 00:01:27,650 --> 00:01:33,670 And then it returns the array, and all of the items are printed out. 23 00:01:33,670 --> 00:01:38,830 Now, when we use the each method with a hash, we get a key and a value. 24 00:01:38,830 --> 00:01:41,461 And we could print it out that way. 25 00:01:47,605 --> 00:01:50,190 So, we should be used to seeing these. 26 00:01:50,190 --> 00:01:52,760 Any class that implements or 27 00:01:52,760 --> 00:01:57,200 includes the enumerable module needs to provide an each method. 28 00:01:57,200 --> 00:02:02,190 And then, once the each method is provided, we get some really 29 00:02:02,190 --> 00:02:06,430 interesting methods that we can do with it for example, member. 30 00:02:06,430 --> 00:02:11,060 So we can say array.member, and pass in an item. 31 00:02:11,060 --> 00:02:12,675 Yes, it does have the number 3. 32 00:02:14,140 --> 00:02:17,140 It does not have the number 30. 33 00:02:17,140 --> 00:02:18,470 Now with a hash. 34 00:02:21,584 --> 00:02:24,180 We can ask if the key is included. 35 00:02:26,480 --> 00:02:30,620 So for example the name key is included, but names are not. 36 00:02:30,620 --> 00:02:33,400 And then we get really cool methods like all. 37 00:02:35,180 --> 00:02:36,980 So let's take a look at our array right here. 38 00:02:36,980 --> 00:02:42,270 And we can say array.all, and then we get this block to pass into it. 39 00:02:43,910 --> 00:02:46,400 And then we could pass in some sort of statement. 40 00:02:46,400 --> 00:02:49,180 Is the item greater than 3? 41 00:02:49,180 --> 00:02:51,660 And no, it's not. 42 00:02:51,660 --> 00:02:59,475 But we could also ask the array if any of the items are greater than three. 43 00:02:59,475 --> 00:03:00,968 And we can do the same thing with hashes. 44 00:03:09,138 --> 00:03:12,210 Now these are methods we've probably seen before. 45 00:03:12,210 --> 00:03:14,560 There's also detect. 46 00:03:14,560 --> 00:03:15,953 So let's go ahead and look at the array again. 47 00:03:21,009 --> 00:03:25,528 And if we call the detect method, pass of the block would be item 48 00:03:25,528 --> 00:03:29,830 being greater than 3, it returns the number four. 49 00:03:29,830 --> 00:03:34,810 Now if we go and look back at the documentation for detect. 50 00:03:34,810 --> 00:03:38,170 We can see that is says it passes each entry to the block, and 51 00:03:38,170 --> 00:03:43,320 returns the first for which the block is not false. 52 00:03:43,320 --> 00:03:48,078 So in our case it returned four because that would be the first 53 00:03:48,078 --> 00:03:52,760 item which met this test which is that it is greater than 3. 54 00:03:52,760 --> 00:03:55,064 And that's pretty much the same as the find method. 55 00:03:59,330 --> 00:04:00,990 Which returns four. 56 00:04:00,990 --> 00:04:03,080 So detect and find are the same. 57 00:04:03,080 --> 00:04:07,268 But, there's also array.find_all. 58 00:04:10,639 --> 00:04:15,600 Which would return 4, 5 and 6, which is the same thing as the select method. 59 00:04:19,110 --> 00:04:20,700 So those are just duplicate methods. 60 00:04:20,700 --> 00:04:25,750 And that works with arrays, hashes and anything that implements enumerable. 61 00:04:25,750 --> 00:04:28,770 But let's get to the interesting methods. 62 00:04:28,770 --> 00:04:30,440 For example the map method. 63 00:04:31,660 --> 00:04:32,990 So here's our array. 64 00:04:32,990 --> 00:04:35,550 It's got these six different items. 65 00:04:35,550 --> 00:04:40,470 And let's go ahead and say we wanted to multiply all of these by 2. 66 00:04:40,470 --> 00:04:47,154 Well, what we could do is say array.map item. 67 00:04:47,154 --> 00:04:50,546 item * 2. 68 00:04:50,546 --> 00:04:54,950 And now we get the multiple of each item in the array. 69 00:04:54,950 --> 00:04:55,590 And then for 70 00:04:55,590 --> 00:05:01,060 some reason let's say we wanted to convert all of these into strings. 71 00:05:01,060 --> 00:05:05,227 Well we can do that to, in fact, if we did our math, we could map it again. 72 00:05:10,923 --> 00:05:14,440 So we get back an array of strings. 73 00:05:14,440 --> 00:05:19,100 Something to keep in mind when working with the map method 74 00:05:19,100 --> 00:05:21,900 is that it will always return an array. 75 00:05:21,900 --> 00:05:27,380 So, if we had our hash, for example, we would say hash.map key, 76 00:05:27,380 --> 00:05:30,650 value, and then, we could just map the values. 77 00:05:30,650 --> 00:05:32,577 And, that would give us back an array of just the value. 78 00:05:32,577 --> 00:05:35,180 So, it should say, Jason, Teacher, Treehouse. 79 00:05:35,180 --> 00:05:36,680 And then, we could do the same thing with the keys. 80 00:05:38,749 --> 00:05:42,717 The important thing to note is that the map method is always going to return 81 00:05:42,717 --> 00:05:44,190 an array in this instance. 82 00:05:45,420 --> 00:05:47,490 So, if we went back up, we could still map it again. 83 00:05:48,540 --> 00:05:56,900 And, transform this result to the number of characters in the word. 84 00:05:56,900 --> 00:06:00,160 So that would be 5, 7, and 9 characters. 85 00:06:00,160 --> 00:06:03,945 Now this winds up being very, very powerful because you can very, 86 00:06:03,945 --> 00:06:12,020 very quickly transform your different arrays, hashes, or any enumerable class. 87 00:06:12,020 --> 00:06:14,030 So here's another really interesting method. 88 00:06:14,030 --> 00:06:18,400 We've got our array, and there's usually a lot of confusion about this method. 89 00:06:18,400 --> 00:06:20,680 And the method is inject. 90 00:06:20,680 --> 00:06:22,991 Let's go ahead and take a look at the inject documentation. 91 00:06:26,895 --> 00:06:28,942 So if we scroll down here, 92 00:06:28,942 --> 00:06:34,620 it says it combines all elements of enum by applying a binary operation, 93 00:06:34,620 --> 00:06:40,040 specified by a block or symbol that names a method or operator. 94 00:06:40,040 --> 00:06:44,450 And then if you specify a block, each element is passed to an accumulator. 95 00:06:44,450 --> 00:06:46,240 What in the world does that mean? 96 00:06:46,240 --> 00:06:49,040 Well, it's actually pretty easy. 97 00:06:49,040 --> 00:06:52,280 And we can think about doing it as a sum. 98 00:06:52,280 --> 00:06:56,750 So we've got an array of integers right here, and 99 00:06:56,750 --> 00:06:59,630 let's say we wanted to add them all up. 100 00:06:59,630 --> 00:07:04,260 We could do array.inject, and 101 00:07:04,260 --> 00:07:09,170 we would say sum, element, and then sum 102 00:07:10,420 --> 00:07:15,670 will be equal to the addition of element. 103 00:07:15,670 --> 00:07:20,230 Now when I press Enter you can see that this returns 21 which 104 00:07:20,230 --> 00:07:24,840 is the sum of every item in this array. 105 00:07:24,840 --> 00:07:29,360 So what happens is, this particular element that goes in as an argument to 106 00:07:29,360 --> 00:07:34,130 the block is, kept between iterations, and then passed to 107 00:07:34,130 --> 00:07:39,740 the next iteration of the block with the same value that it had before. 108 00:07:39,740 --> 00:07:44,490 And we could do the same thing with minus, and that returns -19. 109 00:07:44,490 --> 00:07:50,280 And, we can also, if we really wanted to, specify the beginning value. 110 00:07:51,400 --> 00:07:55,990 So, that beginning value would be passed to the accumulator, 111 00:07:55,990 --> 00:07:57,840 which is the first argument. 112 00:07:57,840 --> 00:08:00,065 So if we did it for zero, it would be the same thing. 113 00:08:04,055 --> 00:08:06,231 We could also start it at say 10. 114 00:08:12,524 --> 00:08:13,248 All right. 115 00:08:13,248 --> 00:08:14,145 Whoop misspelled it there. 116 00:08:22,508 --> 00:08:28,390 And that gives us 31 which is 10 more then we started. 117 00:08:28,390 --> 00:08:33,520 So the inject method can be very, very powerful and save us just a lot of typing. 118 00:08:33,520 --> 00:08:36,720 I mean this is all something that we could write ourselves. 119 00:08:36,720 --> 00:08:37,420 Maybe in a method. 120 00:08:37,420 --> 00:08:41,891 If we really wanted to get it, we would say that the sum zero 121 00:08:41,891 --> 00:08:47,790 array.each item sum += item. 122 00:08:47,790 --> 00:08:50,720 Now if we look at sum, it is 21 just like it was before. 123 00:08:50,720 --> 00:08:55,950 But if we use the inject method, we could pass the result of each item in 124 00:08:55,950 --> 00:09:01,090 the block right back to that accumulator variable without writing our own method, 125 00:09:01,090 --> 00:09:04,310 or even creating that temporary sum variable. 126 00:09:04,310 --> 00:09:07,490 And there are a ton of different methods in enumerable. 127 00:09:07,490 --> 00:09:09,050 We went over just a few of them. 128 00:09:09,050 --> 00:09:13,120 Feel free to go through the documentation and 129 00:09:13,120 --> 00:09:17,760 try all of the different methods just to see which ones you like the best, or 130 00:09:17,760 --> 00:09:22,400 which ones you think might be valuable when coding your Ruby programs. 131 00:09:22,400 --> 00:09:25,940 It's very important to be familiar with the enumerable module, 132 00:09:25,940 --> 00:09:29,770 because a lot of ruby classes and libraries will implement it.