Triple Equals Black Magic

Brandon Weaver
Oct 16, 2017 · 4 min read
Image for post
Image for post

For the most part, === is either ignored or used in the background of case statements. Now there are several articles detailing this, but the question is what all can we use it for?

What is Triple Equal?

In the default case, triple equal is just an alias for double equal. The brilliance comes in when other classes override it for their own behavior. A few examples of this:


(1..10) === 1

For a range, triple equal is an alias for includes?.

Now, a note on ranges, they do a lot of black magic themselves. That’s the subject of a whole new post, but for now a few fun ones that you can try out:



/abc/ === 'abcdef'

For regex, it’s match .


String === 'foo'

For a class, we get is_a?


-> a { a > 5 } === 6

For a proc, we get call. This one is especially interesting for later.

IPAddr'') === ''

Yep, you read that right. IPAddr overrides triple equal to be subnet inclusion, and is even kind enough to coerce whatever you give it into an IPAddr for comparison. SysAdmins will particularly love this one.

Just in Case

Now that we have a few cases of what it does, the prime usage is the case statement in Ruby:

case ''
when'') then 'wired connection'
when'') then 'wireless connection'
else 'unknown connection'

But that’s boring. Surely we can do more with this magical device can’t we? We get duck typing for a reason, why not put it to some use!


Most of you have seen ActiveRecord before, like how we’d query against a Person model:

Person.where(name: 'Bob', age: 10..20)

You know what’d be nice is if we could do something similar to just plain old arrays. Remember how triple equals does things with ranges and regexes and all that? Time to have some fun with it.

Let’s start our experiment with an array of hashes:

people = [
{name: 'Bob', age: 20},
{name: 'Sue', age: 30},
{name: 'Jack', age: 10},
{name: 'Jill', age: 4},
{name: 'Jane', age: 5}

Say we wanted to get everyone that was 20 or older. In regular Ruby we might do something like this: { |person| person[:age] >= 20 }

Though if that had a few more conditions it starts getting a bit hairy, no? This gets to be quite annoying if we’re going through a lot of JSON, so why not borrow from ActiveRecord a bit?

def where(list, conditions = {})
return list if conditions.empty?

Notice how triple equal snuck in there. That means not only can we do exact equality, but we get every single one of those nice matches above as well.

JSON Packet Dump

Say we ended up with some type of packet dump in JSON, we could query against it:

ttl: -> v { v > 30 }

Note though that JSON is going to give you String keys unless you tell it otherwise on parsing, so be careful there.

Not quite enough power? Remember that we have access to Proc as well, which means you could totally use composition to get some really interesting compound queries using something like Ramda in Ruby:

Now we could get away with something nicer like this:


There’s a lot more power there to use, but a subject for another post. Sufficient to say though, Ruby giving us a === on Proc enables some real black magic with a curried framework like Ramda.


That’s well and good, but say that we have objects instead. Well, simple enough. We just switch out item[key] with item.public_send(key) :

def where(list, conditions = {})
return list if conditions.empty?

If one is feeling particularly magical, you can abuse the fact that a Ruby Hash key can be anything to do even worse things:

def where(list, conditions = {})
return list if conditions.empty?

By exploding the key, we could get away with something like this:

where([[1,2,3], [20,30,40]],
[:reduce, 0, :+] =>

Not sure how I’d practically apply that one quite yet, but it’s sure a fun thought in the mean time.

Finishing Up

There are a lot more things you can pull off with triple equals, this is only the tip of that magic iceberg. Combined with functional composition there’s a ton of magic just waiting to be discovered.

Perhaps magic isn’t your thing though, case statements are still useful if not a bit more tame

Ruby Inside

Ruby articles and posts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store