Checking 1.8 vs 1.9 compatibility with Multiruby
For instance, pre-1.9, Ruby's had a problem with local variables in blocks. For instance:
a = 1In Ruby 1.8, this code prints "42", because the block variable
a = 42
ais actually the same
aof the containing scope. Ruby 1.9 fixes this to make blocks behave as expected, i.e. print "1". Of course, if a library's or application's code relied on the old behaviour, it will behave differently on Ruby 1.9.
A part of the Zentest package helps to avoid this: Multiruby. Ryan Davis, creator of Zentest, explains how to handle Ruby 1.8 and 1.9 with Multiruby:
Now that 1.9 is out, it is time to talk about multi-version testing using multiruby. multiruby is a lesser known tool in the zentest family. It automatically builds and privately installs multiple versions of ruby and multiplexes commands to all of them, allowing you to run your tests across multiple versions of ruby all at once.Installing Zentest is simple:
gem install ZenTestMultiruby works as such:
- It will download and compile Ruby versions, eg. 1.8 and 1.9
- Then it executes it's arguments with each one of the available Ruby versions
multirubycommand simply passes all given arguments through to the Ruby binary. Running the code example from above will have this output:
VERSION = 1.8.6-p111As can be seen: the output in 1.8.x is "42", but in 1.9 becomes "1". (The "RESULT" here is the exit code of the Ruby interpreter).
RESULT = 0
VERSION = 1.9.0-0
RESULT = 0
TOTAL RESULT = 0
failures out of 2
Passed: 1.8.6-p111, 1.9.0-0
Naturally, Multiruby will only help to expose problems if a large enough percentage of the code is covered by tests. With ever more Ruby versions, JRuby 1.0 and 1.1, IronRuby and Rubinius becoming available, a tool that simplifies testing across runtimes is quite useful.
Have you started porting or at least testing your applications or libraries with Ruby 1.9?
Martin Thompson Jul 27, 2014