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.


A typo, Sam: in the first line of the paragraph before “require 'test_helper'” you wrote “Ruby 3.0”, but it’s of course “Rails 3.0”.

Posted by Giulio Piancastelli at

Actually, it works with Rails 3.0 and Ruby 1.8.7, but not with Rails 3.0 and Ruby 1.9, so it is Ruby 1.9 that requires the change.  Fixed.  Thanks!

Posted by Sam Ruby at

Interesting about the layout thing.  <%= does buf.concat not +=, so I wonder if that’s the difference?

Posted by Michael Koziarski at

I wonder if that’s the difference?

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 > buffer=english.dup
 => "English" 
ruby-1.9.1-phead > buffer.concat spanish
 => "EnglishEspañol" 
ruby-1.9.1-phead > buffer=spanish.dup
 => "Español" 
ruby-1.9.1-phead > buffer.concat english
 => "EspañolEnglish"

My conclusion: nope.

Posted by Sam Ruby at

Possibly related

Posted by Sam Ruby at

submitted by retardo [link] [comment]...

Excerpt from reddit for ruby hackers at

Ruby Enterprise Edition 1.8.7

The folks at Phusion had put out a new version of Ruby Enterprise Edition 1.8.7, 1.8.7 is more compatible to ruby 1.9 than 1.8.6, also the rubygems had upgraded to version 1.3.5.  This version has been beta tested by the Twitter guys, 37signals...

Excerpt from codename30 at

Sam Ruby: Ruby 1.9 and Rails 3.0

But in any case, I worked around this by simply saying: <%= yield(:layout).force_encoding('utf-8') %>...

Excerpt from Delicious/tag/ruby at

Add your comment