Travails with readline

Applies to: Ruby 1.8.7, compiled from source, on OS X 10.6 (Snow Leopard).

I’ve been working on a side project that deals with Japanese text in Ruby. I installed rtranslate, a gem that gives me programmatic access to Google Translate. I wanted to try it out in irb.

>> require 'rtranslate'
=> true

So far so good.

>> Translate.t("

Here’s where I started to run into problems. I changed the input mode to Japanese and tried to enter はい (hai). All I got was beeping – irb wouldn’t let me enter any Japanese characters at all. I tried pasting from somewhere else – also a no go.

Long story short: by default, Ruby links against editline instead of GNU readline, and editline doesn’t support multi-byte characters. If I started irb with the –noreadline option, it worked fine, but then none of the arrow keys worked. Highly annoying.

I read five different articles [1] [2] [3] [4] [5] that fixed variants of the problem; none of the solutions worked fully for me. My machine is running Snow Leopard, and I had installed Ruby 1.8.7 from source following the Hivelogic directions. So if you’re in the same boat, here’s how you get multi-byte input working:

1. Install GNU readline.

cd ~/src
curl -O
tar xzvf readline-6.0.tar.gz
cd readline-6.0
./configure --prefix=/usr/local
sudo make install

The –prefix in the configure line is crucial.

2. Recompile Ruby, pointing at the new readline. I still had the Ruby source directory in ~/src from when I compiled it the first time (this is a relatively new laptop). If you don’t, you can just download it again.

cd ~/src
curl -O
tar xzvf ruby-1.8.7-p174.tar.gz
cd ruby-1.8.7-p174
make clean
./configure --enable-shared --with-readline-dir=/usr/local
sudo make install

3. My profile had gotten out of whack, so I had to add this line (again) to ~/.bash_profile:

export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH

Now I can enter multi-byte characters into irb, or copy them in from elsewhere. Now I can get on with what I was actually going to do. :)

>> require "rtranslate"
=> true
>> Translate.t("はい", "JAPANESE", "ENGLISH")
=> "Yes"

1 comment to Travails with readline