IRB history not working with Ruby 2.3.0

2019-01-15 06:37发布

问题:

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?

回答1:

OS X's command-line editing is based on the libedit library. OS X has a version of the readline library which is a wrapper around libedit, but it does not behave completely like GNU readline. irb history works in Ruby built with OS X's wrapper up to Ruby 2.1, but Ruby 2.2 and later need to be built with GNU readline for irb history to work.

In the following, 2.3.0 can be any Ruby version from 2.2.0 on. I wrote 2.3.0 since that's what Evan used.

Using MacPorts

rbenv doesn't know about MacPorts, so you need to explicitly tell it to use MacPorts' readline.

  • sudo port install readline if it isn't installed already.
  • rbenv uninstall 2.3.0
  • RUBY_CONFIGURE_OPTS=--with-readline-dir=/opt/local rbenv install 2.3.0

Using Homebrew

rbenv automatically detects homebrew and looks in it for readline, so, if you're using Homebrew and irb history doesn't work, you either haven't installed readline or you built your Ruby before you installed readline.

  • brew install readline if it isn't installed already
  • rbenv uninstall 2.3.0
  • rbenv install 2.3.0