The Spaceship Operator

Let’s take a look below:Using the <=> operator and #group_by, we can transform our original array into a hash containing three key-value pairs: a key of -1 with a value of students before our target, a key of 0 with a value of our target, and a key of 1 with a value of students after our target.

We’ve obtained the desired result, while doing much less!By now, you may have realized the <=> operator is a tool you may not reach for often — in most cases you can substitute Ruby’s #sort method.

However, as shown above, there’s a place for it.

At the very least, you’re bound to stumble upon it in the wild.

When you do, you can approach with confidence knowing that it comes in peace and is here to help.


. More details

Leave a Reply