1 00:00:05,320 --> 00:00:09,120 Ruby as a programming language comes with some different modules 2 00:00:09,120 --> 00:00:10,570 as part of its distribution. 3 00:00:11,700 --> 00:00:15,850 There are two parts to this, Ruby Core and the Ruby Standard Library. 4 00:00:16,870 --> 00:00:19,760 The difference between them is as follows, 5 00:00:19,760 --> 00:00:23,890 Ruby Core contains Ruby language features, while the Ruby standard 6 00:00:23,890 --> 00:00:27,700 library contains different libraries you can use in your Ruby programs. 7 00:00:29,040 --> 00:00:34,250 Something in Ruby Core may only implement a few different core pieces of behavior, 8 00:00:34,250 --> 00:00:36,140 such as file access. 9 00:00:36,140 --> 00:00:40,140 While something in the standard library would implement a suite of behavior, 10 00:00:40,140 --> 00:00:41,550 such as logging functionality. 11 00:00:42,830 --> 00:00:46,010 We're going to take a look now at the comparable module. 12 00:00:46,010 --> 00:00:49,950 The comparable module is a mix-in that provides behavior to help in 13 00:00:49,950 --> 00:00:51,200 ordering your classes. 14 00:00:52,260 --> 00:00:55,220 Let's take a look at how it works now using work spaces. 15 00:00:57,070 --> 00:01:01,440 Okay, so let's go ahead and take a look at the Comparable module. 16 00:01:01,440 --> 00:01:06,160 Now this is part of Ruby Core, and if you head over to the teacher notes, 17 00:01:06,160 --> 00:01:09,880 you'll find a link to the Ruby Comparable module documentation. 18 00:01:11,070 --> 00:01:16,650 And if we look at the documentation, it tells us exactly what Comparable does. 19 00:01:16,650 --> 00:01:21,510 It's used by classes whose objects may be ordered, and 20 00:01:21,510 --> 00:01:25,100 it must define this operator right here. 21 00:01:25,100 --> 00:01:28,900 We haven't seen this before, it's called the spaceship operator. 22 00:01:28,900 --> 00:01:32,326 And it says it compares the receiver against another object, 23 00:01:32,326 --> 00:01:38,460 returning -1, 0, +1, depending on whether or 24 00:01:38,460 --> 00:01:43,010 not the receiver is less than, equal to, or greater than the other object. 25 00:01:43,010 --> 00:01:45,390 So what in the world does that mean? 26 00:01:45,390 --> 00:01:50,880 Well, if it is less then the other object it's being compared to, 27 00:01:50,880 --> 00:01:53,010 we return minus one. 28 00:01:53,010 --> 00:01:57,230 If it's equal to it returns zero, or if it's greater than, it returns plus one. 29 00:01:58,380 --> 00:02:04,360 Now using that, we get some interesting behavior that we can view here. 30 00:02:04,360 --> 00:02:06,870 So, this documentation isn’t that great, 31 00:02:06,870 --> 00:02:11,500 let's go ahead and take a look at how it works, using a player class. 32 00:02:12,500 --> 00:02:17,570 So, right here we have this comparable.rb file open in a workspace, and 33 00:02:17,570 --> 00:02:20,050 we have this player class right here. 34 00:02:20,050 --> 00:02:23,930 And this is pretty simple, the player object has a name and 35 00:02:23,930 --> 00:02:26,160 a score associated with it. 36 00:02:26,160 --> 00:02:31,180 When we initialize the player class, we initialize the name, and the score. 37 00:02:32,740 --> 00:02:36,225 Now, all we have right here is this simple output statement saying, 38 00:02:36,225 --> 00:02:39,570 player1 is greater than player2. 39 00:02:40,720 --> 00:02:45,000 And all this %s right here, means it's a format string, 40 00:02:46,120 --> 00:02:49,390 and anything that's evaluated right here will be returned as a string. 41 00:02:49,390 --> 00:02:53,520 So we're just kind of trying to evaluate the statement, player1 > player2. 42 00:02:53,520 --> 00:02:57,350 Now let's go ahead and run this, and see what happens. 43 00:02:59,300 --> 00:03:04,650 We have undefined method greater than for this player class, and that makes 44 00:03:04,650 --> 00:03:10,070 sense because we haven't done anything with the Comparable module just yet. 45 00:03:11,110 --> 00:03:12,340 So, here's how we do it. 46 00:03:12,340 --> 00:03:16,610 We can say include Comparable. 47 00:03:18,190 --> 00:03:24,170 Now, that will include the Comparable module into our player class. 48 00:03:24,170 --> 00:03:29,190 Mixing in all of the different methods that the Comparable module gives us. 49 00:03:29,190 --> 00:03:31,670 And let's run this again and see what happens. 50 00:03:31,670 --> 00:03:35,080 Once again we got a failure, but this is a different error. 51 00:03:35,080 --> 00:03:40,880 It says comparison of player with player failed, and why did it fail? 52 00:03:42,070 --> 00:03:45,950 It's because we didn't implement this spaceship operator. 53 00:03:47,240 --> 00:03:51,877 So, let's go ahead and implement that now, and we define that like any other method. 54 00:03:56,677 --> 00:04:01,451 And we can do a little short circuit here, and 55 00:04:01,451 --> 00:04:09,759 we can just say score is the spaceship operator to the other_player.score. 56 00:04:10,800 --> 00:04:15,020 So if one of us has a score of 100 and the other has a score of 80, 57 00:04:15,020 --> 00:04:18,570 that should return one, 58 00:04:18,570 --> 00:04:24,130 because one is greater than the other side of this operand. 59 00:04:24,130 --> 00:04:29,420 So I'll clear my screen here and run this again, and you'll see we now have this 60 00:04:29,420 --> 00:04:33,640 method, saying player1 is greater than player2, that evaluates to true. 61 00:04:35,810 --> 00:04:40,490 And the same thing will work, if we do it with the less than sign here. 62 00:04:42,090 --> 00:04:46,240 We now get that method for free, and if we look back in the documentation for 63 00:04:46,240 --> 00:04:50,190 Comparable, we get all of these different methods right here. 64 00:04:50,190 --> 00:04:54,610 Less than, less than or equal to, equal to, greater than, greater than or 65 00:04:54,610 --> 00:04:57,550 equal to, and between. 66 00:04:57,550 --> 00:05:01,645 So, we could also see if something was between something else. 67 00:05:01,645 --> 00:05:04,325 And the nice thing about this is, we didn't have to write 68 00:05:04,325 --> 00:05:08,125 any code to see whether or not this was greater than something else. 69 00:05:08,125 --> 00:05:11,145 By just defining this one spaceship operator, 70 00:05:11,145 --> 00:05:13,925 we were able to get all of this behavior for free.