Evan Pon Evan Pon - 7 months ago 59
Ruby Question

IRB history not working with Ruby 2.3.0

I have Ruby 2.3.0p0 installed via rbenv, on OS X 10.11.4. Within an IRB session, history works fine. However, I cannot access IRB history across sessions.

I tried my system Ruby, 2.0.0p648, and history across IRB sessions works fine. I tried installing that same version of Ruby via rbenv, and it also has working history.

I've compared the values of IRB.conf between a working and nonworking session, and nothing looks out of place (although weirdly, irb/ext/save-history.rb is a blank file in both cases).

Looking at my .irb_history file, it appears that it is getting replaced, rather than appended, with the commands from the latest session. I can load up a 2.0.0 IRB session and scroll through those commands just fine.

I've tried the answers listed in rbenv irb history is not saving without success.

I also tried the selected answer in irb history not working.
I had to alter the selected answer to replace the #nitems method. This showed that lines were being read out from the history file, and pushed on to Readline::HISTORY. However, examining Readline::HISTORY shows nothing there.

I can sort of hack in history by adding previous lines from my .irb_history to the Readline history via Readline.readline, and specifying add_hist=true. However, it is definitely not the proper way to add the previous commands to Readline.

I know I can switch over to something like pry, but I'd like to figure this out.
Any suggestions on why the commands are not being added to Readline, and how to change that?


Coincidentally I worked this exact issue out on my own system yesterday. The problem was that my Ruby wasn't built against real readline, but against OS X's fake readline (a wrapper around another library).

I use MacPorts and I already had a current readline on my system; Ruby just wasn't built against it. Building Ruby 2.3.1 with

RUBY_CONFIGURE_OPTS=--with-readline-dir=/opt/local rbenv install 2.3.1

made irb history work for me.

rbenv automatically detects homebrew and looks in it for readline, so if you have homebrew but haven't installed readline, do that and then uninstall and rebuild Ruby. I haven't verified this myself, but I'm pretty sure it's true because I'm pretty sure irb history worked fine on a different project which used homebrew.

irb history works correctly with Mac 'readline' up to 2.1; real readline is necessary in Ruby 2.2 and later.