Last Updated: February 25, 2016
·
3.536K
· defeated

Enable unicode support in IRB

Recently I tried to paste some unicode characters into my irb session, but was greeted with the following:

irb(main):001:0> utf8_str = "r\U+FFC3\U+FFA9sum\U+FFC3\U+FFA9"

That's not what I expected! After some intrepid googling, this article by Henrik Nyh says the default OS X libedit library that Ruby is compiled against doesn't have good unicode support.

I wanted to confirm that rbenv and ruby-build had linked to libedit instead of a newer version of readline.

$ cd ~/.rbenv/versions/1.9.3-p374/lib/ruby/1.9.1/x86_64-darwin12.2.0
$ otool -L readline.bundle 
  readline.bundle:
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)

Let's fix that. Update homebrew and install the latest readline lib:

$ brew update && brew install readline

Then, recompile ruby by manually setting the path to readline using the CONFIGURE_OPTS environment variable:

CONFIGURE_OPTS=--with-readline-dir=`brew --prefix readline` rbenv install 1.9.3-p374

We can confirm readline was linked by running otool again:

$ otool -L readline.bundle 
  readline.bundle:
    /usr/local/opt/readline/lib/libreadline.6.2.dylib (compatibility version 6.0.0, current version 6.2.0)

4 Responses
Add your response

Or just start using Pry.

over 1 year ago ·

Thank you very much for this.

I had to do one other thing: make sure that the $LANG environment variable is set properly.

In my case (Mac OS X), this was blank, probably because of a Custom region setting in System Preferences.

Setting $LANG to something sensible (e.g., en_GB.UTF-8) fixed this longstanding problem on my machine.

For full details, see the unicodetitlecase wiki at https://github.com/cantab/unicodetitlecase/wiki/_pages

over 1 year ago ·

cantabip -

good to know, thanks for mentioning!

I also want to point out, Tim Pope has basically rolled up all the readline build integration into this rbenv plugin:
https://github.com/tpope/rbenv-readline

over 1 year ago ·

Thanks!

over 1 year ago ·