If you’re like me, you still have some Rails 2.3.x apps hanging around. Yeah, Rails 3 is awesome in some respects, but that “2x performance increase” never quite materialized.
In fact, for the most part, Rails 3 is still slower than Rails 2.3. SIGH!
Nevertheless, Ruby itself is progressing nicely. Ruby 1.9.3-p0 was released a few months ago. With faster garbage collection, startup, and file load times, Ruby 1.9.3 is where it’s at.
The only question is: What do you do to make Rails 2.3 play nicely with Ruby 1.9.3?
It’s actually not that hard!
First of all, if you’re still on 1.8.7, see my post How to Make Your Rails 2.3 App Work with Ruby 1.9.
Besides the stuff I outlined in the above post (for Ruby 1.9.2), there are a few more things you’ll need to keep in mind to get up and running with Ruby 1.9.3.
The first thing to do before compiling Ruby 1.9.3 from source is that you may need to install libyaml (or libyaml-dev on Ubuntu):
sudo apt-get install libyaml-dev
Now when you compile Ruby 1.9.3, and then try to install Rails, it won’t give you an error message saying something like, “Oh crap, you don’t seem to have Psych installed”.
Psych is the new fast YAML-cruncher that replaces Syck, the old one.
And Psych is the precise reason you will have some YAML headaches when you try to fire up your Rails 2.3.x app with Ruby 1.9.3.
Fixing the problem is pretty easy, though. After you install Ruby 1.9.3, reinstall all your gems – including Rails 2.3.x. Then, find this file and edit it:
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-2.3.10/lib/active_support/locale/en.yml
Note that your ruby may not be installed in /usr/local/ruby/, but just do “which ruby” to see where it lives. Then, edit this en.yml file to look like so:
en: date: formats: # Use the strftime parameters for formats. # When no format has been given, it uses default. # You can provide other formats here if you like! default: "%Y-%m-%d" short: "%b %d" long: "%B %d, %Y" day_names: - Sunday - Monday - Tuesday - Wednesday - Thursday - Friday - Saturday abbr_day_names: - Sun - Mon - Tue - Wed - Thu - Fri - Sat # Don't forget the nil at the beginning; there's no such thing as a 0th month month_names: - ~ - January - February - March - April - May - June - July - August - September - October - November - December abbr_month_names: - ~ - Jan - Feb - Mar - Apr - May - Jun - Jul - Aug - Sep - Oct - Nov - Dec # Used in date_select and datime_select. order: - !ruby/symbol year - !ruby/symbol month - !ruby/symbol day time: formats: default: "%a, %d %b %Y %H:%M:%S %z" short: "%d %b %H:%M" long: "%B %d, %Y %H:%M" am: "am" pm: "pm" # Used in array.to_sentence. support: array: words_connector: ", " two_words_connector: " and " last_word_connector: ", and "
The difference is that stuff like this:
day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
Needs to be changed to look like this:
day_names: - Sunday - Monday - Tuesday - Wednesday - Thursday - Friday - Saturday
There are a few other changes, too. Another shortcut way of figuring out how to edit your YAML files to be Psych-friendly is to copy/paste them into YAMLlint.com. Click GO and it will clean them for you. Then copy/paste back into your app, and you’re done.
Note that you may also have to edit your config/database.yml file (watch out for stray spaces) and any locale files you may have in config/locales if you have a multi-lang app.
Last, when you start your app, you’ll get an error when Rails can’t find NAME_helper.rb files for all your views. Not to worry. Create a new file:
config/initializer/fix_file_parsing_error.rb
And then just put this inside:
# encoding: utf-8 # For Ruby 1.9.3 compatibility MissingSourceFile::REGEXPS.push( [/^cannot load such file -- (.+)$/i, 1] )
Okeydokey.
Now, let ‘er rip, and you should have the Ruby 1.9.3 goodness – including sassier garbage collection and faster file load times – in your Rails 2.3.x app!
I just wanted to put this out there that although you can get Rails 2.3 running on Ruby 1.9.3, if you have any RSpecs, they will be FUBAR, and they will generate a tonne of “Cannot modify frozen XXX” errors, and they come from within the RSpec library itself.
For really massive projects, where RSpecs are a requirement of the job, this is (unfortunately) a deal-breaker.