---
title: 'The Journey to Ruby 1.9 '
teaser:
tags: web,ruby
author: Nick Quaranto
published_on: 2009-07-17
---

Thanks to some late-night hacking at [Boston.rb](http://bostonrb.org) and plenty
of awesome contributors, our gems are now Ruby 1.9 compatible. If you’re
wondering what’s different in the newest version of Ruby, check out [this great
list from Eigenclass.org](http://eigenclass.org/hiki/Changes+in+Ruby+1.9).
Here’s some tips and tricks for those who want to upgrade their own Ruby install
and have their gems to be compatible.

[![''](http://farm4.static.flickr.com/3173/2409347498_8600da9635_m.jpg)](http://www.flickr.com/photos/rextsai/2409347498/)

## Installation

It’s dead easy to run Ruby 1.8.\* and 1.9 together on the same machine. Check
out how to install it on OS X
[here](http://programblings.com/2008/11/18/installing-ruby-19preview1-on-os-x-leopard/),
or on Ubuntu
[here](http://www.rubyhead.com/2009/01/11/installing-ruby-191-preview-1-rails-on-ubuntu-804lts-hardy-heron/).
If you’ve got a great blog post on how to install it on your favorite OS, drop a
comment. The most important thing to remember is when you `configure` your
install, add

`--program-suffix=1.9`

as a flag so all of the binaries end in `1.9`. Also, don’t forget to grab the
[latest RubyGems version](http://rubyforge.org/frs/?group_id=126). Once you’ve
got it downloaded and unpacked, install with:

`sudo ruby1.9 setup.rb --format-executable`

Make sure to tack on `--format-executable` whenever you do a `gem1.9 install`.
This will setup executables with the 1.9 suffix, so for instance you can have
`cucumber` and `cucumber1.9` living harmoniously together.

## Gotchas

Here’s a list of some of the roadbumps we ran into while upgrading our gems, and
hopefully you can keep an eye out for them in your code as well.

### Files with non-ASCII characters need an encoding set

The basic solution to this is adding `# encoding: utf-8` to the top of your
file. Ruby just won’t parse the file without it. Of course, if your file has a
different encoding you’ll need use that instead.

### `lambda` is strict about arity

    % irb
    irb(main):001:0> lambda { 2 + 2 }.call(13)
    => 4
    % irb1.9
    irb(main):002:0> lambda { 2 + 2 }.call(13)
    ArgumentError: wrong number of arguments (1 for 0)
        from (irb):2:in `call'
        from (irb):2
        from /opt/local/bin/irb1.9:12:in `<main>'

We saw this in
[Paperclip](http://github.com/thoughtbot/paperclip/commit/318d0b2908393a908957f6b4a43eb3cb116eacb4)
and [Factory
Bot](http://github.com/thoughtbot/factory_bot/commit/452676dfc43928d50ccb3b9e825b5f1caa413feb).
The solution is to make sure you’re calling the lambda with the right amount of
arguments, and checking `#arity` if you have to.

### Block-local variables don’t leak scope

This has probably been mentioned on every Ruby 1.9 blog post in existence, but
it’s still worth noting.

    % irb
    irb(main):001:0> item = 1
    => 1
    irb(main):002:0> lambda { |item| }.call(2)
    => nil
    irb(main):003:0> item
    => 2

    % irb1.9
    irb(main):001:0> item = 1
    => 1
    irb(main):002:0> lambda { |item| }.call(2)
    => nil
    irb(main):003:0> item
    => 1

Basically, just make sure that you’re [naming things
properly](https://thoughtbot.com/blog/sign-up-sign-in-sign-out). Here’s [where
we](http://github.com/thoughtbot/factory_bot/commit/452676dfc43928d50ccb3b9e825b5f1caa413feb)
[fixed
it](http://github.com/thoughtbot/paperclip/commit/318d0b2908393a908957f6b4a43eb3cb116eacb4).

### `#instance_methods`, `#methods`, and their cousins now return symbols, not strings

    % irb
    irb(main):001:0> Object.methods[0..5]
    => ["private_class_method", "inspect", "name", "tap", "clone", "public_methods"]

    % irb1.9
    irb(main):001:0> Object.methods[0..5]
    => [:allocate, :new, :superclass, :freeze, :===, :==]
    </pre>

The basic fix for this is to tack `.map(&amp;:to_s)` onto the call. This was
happening in
[quite](http://github.com/thoughtbot/paperclip/commit/800f383a0ae97e1bae0941b129acf6f6030d09c1)
[a](http://github.com/thoughtbot/hoptoad_notifier/commit/eee95a901285bc966854d26ce6b5b522fd80bc86)
[few](http://github.com/thoughtbot/hoptoad_notifier/commit/7b90cea9bdf97c6f6173edfc0a43546df76d177d)
gems.

### `Array#to_s` does more of an inspect instead of its behavior in 1.8

    % irb
    irb(main):001:0> ["test", nil].to_s
    => "test"

    % irb1.9
    irb(main):001:0> ["test", nil].to_s
    => "[\"test\", nil]"

We saw this in [one of Paperclip’s
tests](http://github.com/thoughtbot/paperclip/commit/4f6846126534b6debe20e6d3a3c3c29d6682b497).
While the new behavior seems to make more sense, just watch out if old code
depends on it.

### The `__FILE__` constant is an absolute path in tests

This example is a default [jeweler](http://github.com/technicalpickles/jeweler)
generated gem with `p __FILE__` in a test:

```shell
% rake
(in /Users/qrush/Dev/ruby/test-gem)
/opt/local/bin/ruby -I"lib:lib:test" "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
  "test/test-gem_test.rb"
Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
"./test/test-gem_test.rb"
.
Finished in 0.000371 seconds.

% rake1.9
(in /Users/qrush/Dev/ruby/test-gem)
Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler
/opt/local/bin/ruby1.9 -I"lib:lib:test" "/opt/local/lib/ruby1.9/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
  "test/test-gem_test.rb"
Loaded suite /opt/local/lib/ruby1.9/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
"/Users/qrush/Dev/ruby/test-gem/test/test-gem_test.rb"
.
Finished in 0.000728 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
```

The solution to this was to use `File.expand_path` to make sure that it worked
on both versions. This happened in
[Paperclip’s](http://github.com/thoughtbot/paperclip/commit/8a40d68c92e136c3ad8d2157fa4ecbc813136780)
tests as well.

[![''](http://www.classicvinylrecord.com/wp-content/uploads/2008/01/journey-escape.jpg)](http://www.classicvinylrecord.com/journey-escape-album-on-lp/)

### Onward

We test all of our gems on 1.8.7 and 1.9.1 on our [Integrity CI
server](http://ci.thoughtbot.com/) as well, if you’re curious. As of today we’re
not yet running full time on 1.9, but we hope to be there soon. The main reason
we’re not is because some gems/plugins aren’t compatible, and some hosts don’t
support it. We’ll post more articles in the future once we’re running our Rails
projects on Ruby 1.9.
