Sitemap
Matthew Dunn

Matthew Dunn

I am 6"8 but can’t dunk, so I write about code instead.

Comparing Objects in Ruby

Using the Comparable module/mixin

3 min readMar 26, 2016

--

In the Ruby programming language, what it means to “compare A to B” is either downright obvious, or at least intuitively so. A Ruby programmer simply knows/expects that if they type 5 < 10, the Ruby interpreter will resolve this expression to true. Perhaps slightly less obvious, but nonetheless intuitive is the fact that the Ruby interpreter will tell you that ‘a’ < ‘z’.

What about more complex objects?

How do we compare more than simple numbers or strings?

That is all fine and well, but what happens when you want to compare more complex objects (i.e. objects that you have built yourself). What if, for instance, we were writing an application that required us to compare one surfer to another. Look at this code for a moment…

What are we supposed to be comparing here? Years spent surfing? The biggest wave ever surfed? Marco has spent more years surfing, but Ken has surfed the bigger wave…

Get Matthew Dunn’s stories in your inbox

Join Medium for free to get updates from this writer.

We teach our objects to compare themselves to one another using the Comparable module/mixin available in Ruby (if you are not familiar with modules in Ruby, please feel free to read another post I recently wrote that covers both the purpose of modules as well as how to use the Enumerable module/mixin). From the documentation:

The Comparable mixin is used by classes whose objects may be ordered. The class must define the <=> operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. If the other object is not comparable then the <=> operator should return nil. Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method between?.

If you are not aware, the <=> is referred to as the “spaceship operator” and does exactly what the documentation says: it returns -1, 0, or 1 depending on the result of the comparison with another object. Those values are in turn used by the various comparison operators that are made available in the Comparable mixin (please check the documentation of an individual method/operator in the documentation if that is not clear).

Using the Comparable module/mixin

Defining the spaceship operator for great good

So how do you teach your objects to compare themselves to other objects? Again, the documentation is instructive — we have to define the spaceship operator. Instead of delving into any more theory, let’s revise our prior code a bit:

Let’s assume that our business rules are such that comparing one surfer to another means to compare the years they have been surfing. We could always reach into the individual objects and compare the attributes as numbers, but wouldn’t it be nicer (and more Ruby-esque) if the objects simply knew how to compare themselves?

The new code does two things — first it includes the Comparable module. Next, it defines the spaceship operator method and within the body of that method uses a technique referred to as “delegation” — our method simply delegates to the individual objects and whatever property we have chosen to compare.

By doing this and this alone, we now have access to the most commonly used comparison operators (i.e. <, >, <=, ==, >=, etc.) as well as an awesome method called #between?, which does what you would likely intuit, which is to say that it returns a boolean value indicating whether the object upon which the method was called is between the first and second parameters passed into the method.

Summary

Thanks for sticking with me…

Hopefully this has taught you a bit about how to teach your objects some new tricks in Ruby. As always, I am more than grateful for any and all feedback and if you like this article, please feel free to heart it!

--

--

Matthew Dunn
Matthew Dunn

I am 6"8 but can’t dunk, so I write about code instead.