Moving to 1.9
On Ruby 1.8, strings are sequences of bytes. On Ruby 1.9, strings are proper characters (not bytes!). Even if your app only speaks “American”, you still need to be aware of this to handle data properly. Plus, some of the new syntax in 1.9 is not backwards compatible with 1.8.
Recommended steps for upgrading from 1.8 to 1.9:
- make sure you have good test coverage!
- make sure your test are checking the output (some end-result validation)
- run on 1.9
- hammer on your code until the tests pass
- decide whether to continue to support 1.8
Prawn only officially supports 1.8.6 and 1.9.1 to make life easier, but if support more versions is necessary for your project, check out ZenTest’s multiruby features.
Greg recommends using conditional-execution blocks to make version-dependent code look nicer:
if RUBY_VERSION < "1.9" def ruby18 yield end else def ruby18 end end
Greg opines that moving to Ruby 1.9 is not a magic bullet, but has lots of advantages, so try it out!
Ruby 1.8.6 is a workhorse (insert image of beat-up pickup truck). Ruby 1.9 is a Lamborghini (we think). “What the hell is 1.8.7?”
Answer: 1.8.7’s patch set is largely 1.9 backports. It’s a platypus!
However, this doesn’t mean that code written for 1.9 will magically work on 1.8.7. Or that code written for 1.8.7 will work on 1.8.6.
What should authors be doing? Should we release for 1.8.6 or 1.8.7? Greg recommends releasing for 1.9, especially if you’re writing a Ruby book (wink wink).
FFI (Foreign Function Interface) is supported “all over the place”, and is an alternative to writing a C extension. FFI works across implementations (JRuby, Rubinius, and MRI).
On Windows, Greg proclaims that JRuby is the easiest way to wrap a C library. “WTF?”
Oversimplified Explanations of Ruby Variations
According to Greg. (Not all of the nuance may be captured here, since Greg was moving pretty quickly. Blame me, not him.)
- 1.8.6 is ubiquitous, and may be slowing adoption of other, better interpreters.
- YARV (1.9) is faster than Matz’s implementation and is the only complete m17n implementation of Ruby.
- Ruby Enterprise has a great installer!
- JRuby is great and new, but requires C extensions to be rewritten
- Rubinius is what created the RubySpec project and FFI, and is very innovative.
- MacRuby is, um, Ruby for Macs.
About the Author