Forward Compatibility with Ruby 1.9

Rails 3.0 is going to fully support Ruby 1.9. It’s a good thing, too, since 1.9 is much faster and has some important improvements. What that means is that, eventually, if not now, you’ll want to at least make sure your code runs in 1.9. A while back, I went through the differences between 1.8 and 1.9 with an eye for forward compatibility issues. I’ve picked out what I think the major gotchas are. Avoid these in your code and it should make moving to 1.9 a lot easier.

Hashes are now ordered. This really shouldn’t cause problems for you, since the order of hashes in 1.8 are not to be trusted. You may want to consider an alternate implementation of OrderedHash once you go to 1.9 so eliminate unnecessary work.

Object now has a superclass, BasicObject. You should checkout the Pickaxe for the reasons behind this, but it’s important that you don’t rely on Object.superclass being nil.

The grammar for single line if statements has changed. Specifically, then is now the blessed form. The : form has been removed.

The retry command has been removed from looping. It is still available for exception handling, though. I doubt anybody here really used this. Don’t start.

The scoping rules for blocks have changed. This is probably the most likely to bite you because it’s an easy thing to miss. The change is that, in 1.9, block parameters are always private to the block. The best way to protect yourself from this is to never share a local variable name with a block parameter name.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>