intertwingly

It’s just data

Ruby 1.9 and Rails 3.0


Much to my surprise, it looks like these two will be ready for each other.  The latest release is Ruby 1.9.2 Preview 1, and blows up and spills its entrails the first time you try to generate scaffolding.  But undaunted, I tried the latest Ruby head against the latest Rails head.  The only failure you see is the same one that I get with Rails head and Ruby 1.8.7 and the subject of this ticket.

Only two user visible changes.  The first minor, the default formatting for dates has changed:

ruby-1.8.7-p174 > Time.now.to_s
 => "Mon Sep 21 07:48:02 -0400 2009" 
ruby-1.9.1-phead > Time.now.to_s
 => "2009-09-21 07:48:02 -0400"

The second is more serious, but likely to be fixed over time.  In the chapter on Internationalization, if a layout has non-ASCII characters and a given template only has ASCII characters, I get a message like the following:

Encoding::CompatibilityError in Store#checkout

Showing /home/rubys/git/awdwr/work/depot/app/views/layouts/store.html.erb where line #66 raised:

incompatible character encodings: UTF-8 and US-ASCII

I would have thought that UTF-8 and US-ASCII were compatible:

ruby-1.9.1-phead > english='English'.force_encoding('US-ASCII')
 => "English" 
ruby-1.9.1-phead > spanish="Espa\xc3\xb1ol".force_encoding('UTF-8')
 => "Español" 
ruby-1.9.1-phead > english+spanish
 => "EnglishEspañol" 
ruby-1.9.1-phead > spanish+english
 => "EspañolEnglish" 
ruby-1.9.1-phead > (english+spanish).encoding
 => #<Encoding:UTF-8> 
ruby-1.9.1-phead > (spanish+english).encoding
 => #<Encoding:UTF-8>

But in any case, I worked around this by simply saying:

<%= yield(:layout).force_encoding('utf-8') %>

This seemed easier than addressing each and every template.

Along the way, I encountered a single Rails bug, easily fixed.  A number of other minor changes were required, for example early versions of Rails required ugly things like the following at times:

require File.dirname(__FILE__) + '/../test_helper'

In later versions of Rails, this still worked, but wasn’t necessary.  With Ruby 1.9, this will cease to work, and the following will work with Rails 2.3.4 on 1.8.7 as well as Rails 3.0 on Ruby 1.9.2:

require 'test_helper'

Other changes were truly minor, like declaring the #encoding on source files that contain non-ASCII characters, adjusting test case verification to accommodate other differences in output, and some other tightening of the language (e.g., use of super with no arguments in define_method, eval scope).

All in all, I have a single scenario with some minor “if” checks that works any combination of Ruby 1.8.7 or 1.9.2 with Rails 2.3.4 or Rails 3.0.  I’m now making the working assumption that Edition 4 of Agile Web Development with Rails will target Ruby 1.9.2 (which if tradition holds, will be available at Christmas 2009), but will work with only minor differences noted in the book with Ruby 1.8.7.