Search code examples
rubyrubocop

How to check for API compatibility between Ruby versions?


Our team maintains a ruby script that must support any ruby >= 1.9.3 (yes, that's a pretty old ruby version but this is something outside our control at the moment). Recently, we added a change that included the verification of a regexp using match? and everything was going well until one of the users reported that the script was exploding for ruby 2.3 with the message:

undefined method `match?' for #<Regexp:0x000000018471e0> (NoMethodError)

Turns out this happens because the match? method is available in ruby >= 2.4.

We have been running ruby -c script.rb with multiple versions of ruby (1.9.3, 2.0.0, 2.1.10, 2.3.7, 2.5.1, 2.6.3, 2.7.4) as part of our test suite and this didn't show up as a syntax error and I suspect it's because the actual syntax is "correct" but the method is simply not available in some versions of ruby. We also tried rubocop, hoping that it could detect these incompatibility issues but it doesn't report any error either.

So, my question is, what's the best way to check for this API compatibility issues between multiple ruby versions?


Solution

  • Running ruby -c with the oldest Ruby you support is a good start, as it will detect if you are trying to use a newer syntax.

    For using methods that are new:

    1. Run your tests suite in both the oldest and newest Ruby versions you want to support.

    2. Consider using my backports gem to make it possible to use newer API even on old Rubies. For example, it includes Regexp.match? among the hundreds of backports...