Comparable5:14 with Jason Seifer
For our first stop on the Ruby Core Module tour, we're going to check out the `Comparable` module. The `Comparable` module allows you to make your classes sortable and gives you convenience methods.
By including the
Comparable class and defining the spaceship operator(
<=> ), we get access to comparison functionality in our classes:
class Player include Comparable attr_accessor :name, :score def <=>(other_player) score <=> other_player.score end def initialize(name, score) @name = name @score = score end end player1 = Player.new("Jason", 100) player2 = Player.new("Kenneth", 80) puts "player1 > player2: %s" % (player1 > player2) puts "player1 < player2: %s" % (player1 < player2)
Ruby as a programming language comes with some different modules 0:05 as part of its distribution. 0:09 There are two parts to this, Ruby Core and the Ruby Standard Library. 0:11 The difference between them is as follows, 0:16 Ruby Core contains Ruby language features, while the Ruby standard 0:19 library contains different libraries you can use in your Ruby programs. 0:23 Something in Ruby Core may only implement a few different core pieces of behavior, 0:29 such as file access. 0:34 While something in the standard library would implement a suite of behavior, 0:36 such as logging functionality. 0:40 We're going to take a look now at the comparable module. 0:42 The comparable module is a mix-in that provides behavior to help in 0:46 ordering your classes. 0:49 Let's take a look at how it works now using work spaces. 0:52 Okay, so let's go ahead and take a look at the Comparable module. 0:57 Now this is part of Ruby Core, and if you head over to the teacher notes, 1:01 you'll find a link to the Ruby Comparable module documentation. 1:06 And if we look at the documentation, it tells us exactly what Comparable does. 1:11 It's used by classes whose objects may be ordered, and 1:16 it must define this operator right here. 1:21 We haven't seen this before, it's called the spaceship operator. 1:25 And it says it compares the receiver against another object, 1:28 returning -1, 0, +1, depending on whether or 1:32 not the receiver is less than, equal to, or greater than the other object. 1:38 So what in the world does that mean? 1:43 Well, if it is less then the other object it's being compared to, 1:45 we return minus one. 1:50 If it's equal to it returns zero, or if it's greater than, it returns plus one. 1:53 Now using that, we get some interesting behavior that we can view here. 1:58 So, this documentation isn’t that great, 2:04 lets go ahead and take a look at how it works, using a player class. 2:06 So, right here we have this comparable.rb file open in a workspace, and 2:12 we have this player class right here. 2:17 And this is pretty simple, the player object has a name and 2:20 a score associated with it. 2:23 When we initialize the player class, we initialize the name, and the score. 2:26 Now, all we have right here is this simple output statement saying, 2:32 player1 is greater than player2. 2:36 And all this %s right here, means its a format string, 2:40 and anything that's evaluated right here will be returned as a string. 2:46 So we're just kind of trying to evaluate the statement, player1 > player2. 2:49 Now let's go ahead and run this, and see what happens. 2:53 We have undefined method greater than for this player class, and that makes 2:59 sense because we haven't done anything with the Comparable module just yet. 3:04 So, here's how we do it. 3:11 We can say include Comparable. 3:12 Now, that will include the Comparable module into our player class. 3:18 Mixing in all of the different methods that the Comparable module gives us. 3:24 And let's run this again and see what happens. 3:29 Once again we got a failure, but this is a different error. 3:31 It says comparison of player with player failed, and why did it fail? 3:35 It's because we didn't implement this spaceship operator. 3:42 So, let's go ahead and implement that now, and we define that like any other method. 3:47 And we can do a little short circuit here, and 3:56 we can just say score is the spaceship operator to the other_player.score. 4:01 So if one of us has a score of 100 and the other has a score of 80, 4:10 that should return one, 4:15 because one is greater than the other side of this operand. 4:18 So I'll clear my screen here and run this again, and you'll see we now have this 4:24 method, saying player1 is greater than player2, that evaluates to true. 4:29 And the same thing will work, if we do it with the less than sign here. 4:35 We now get that method for free, and if we look back in the documentation for 4:42 Comparable, we get all of these different methods right here. 4:46 Less than, less than or equal to, equal to, greater than, greater than or 4:50 equal to, and between. 4:54 So, we could also see if something was between something else. 4:57 And the nice thing about this is, we didn't have to write 5:01 any code to see whether or not this was greater than something else. 5:04 By just defining this one spaceship operator, 5:08 we were able to get all of this behavior for free. 5:11
You need to sign up for Treehouse in order to download course files.Sign up