Ruby, types, and interfaces
The other day I was trying to understand why a test was failing. Spelunking through the code I ended up looking at one of the gems the app uses. In its code, I ran into something similar to this:
The idea being that the gem would call this method to test a URL path against a user-provided object, which—as you can see from the code—can be of many different types.
Looking at that, it got me thinking: Did the author(s) of this code really care about whether the filter was a Proc, or just any callable would've worked? Did they really care whether it was a RegExp?
If someone implements an Array look-alike without inheriting (because, you know, inheriting from ruby’s core classes is fraught with peril), do we care? Or do we just care that we get an object that responds to join?
For a language with such a strong advocacy of “duck typing”, there’s a lot of production code that just depends on the specific class of an object instead of caring about the messages it understands.
Can we improve this?
What would be the best way to check the messages each filter can respond to, instead of their explicit type?
An obvious first approach would be using if statements:
This is, however, a lot harder to read. Seeing a case in ruby immediately communicates that there's some pattern matching coming, instead of just "a big conditional."
Get Nicolás Sanguinetti’s stories in your inbox
Join Medium for free to get updates from this writer.
So, here’s a (tiny) experiment to preserve the pattern matching feeling:
Now we’re not restricting ourselves to the types we pass, but the interfaces they implement. Anything that implements call, or match, for example, will work. 👌
Success?
Not everything that shines is gold, however. And not everything that implements a method named join will behave similarly.
Enter Pathname. If this method could potentially receive a Pathname instead of an Array of strings, you'd have a fun bug to catch:
You could work around this by saying Implements[:join, :each], to make sure we only target “joinable enumerables”.
However, that stops being transparent. It’s no longer obvious why we’re asking for things that implement join and each but then just call join on them. At this point we have to ask the same question we started with: In this case, do we care about the type of the thing, or the interface of the thing?
And in this particular case, the answer is probably that we care about the type (“a list of strings that give segments of a path”) rather than the interface.
So should we use this, or not?
The answer, surprisingly, is “maybe.” 😛
I’m not going to run and send pull requests for every type-matching case statement in the wild. And neither should you.
As always, you should be careful and think of the consequences of every single line of code you add (or remove).
Particularly, you should think about what each line of code communicates to its readers—whether that’s future you, your team, or a user of your library that’s beffuddled by a failing test.
(Which, by the way, had nothing to do with this code. Ended up being a stupid mistake I was doing elsewhere… as usual.)








