IRB history not working with Ruby 2.3.0
Asked Answered
E

1

25

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?

Emulous answered 16/6, 2016 at 0:12 Comment(2)
One thing you said seems strange to me: you said save-history.rb is blank. My ~/.rbenv/versions/2.3.0/lib/ruby/2.3.0/irb/ext/save-history.rb is full of code which makes history work. I'd think if yours was blank, irb history wouldn't work with or without readline.Ticktock
Hmm. I don't have my original 2.3.0 anymore, but I just went back and looked at my 2.0.0, and the save-history.rb file does indeed have code in it. I don't have an explanation, other than that my brain was a bit fried from digging around on this.Emulous
T
42

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 Homebrew

If you install ruby using homebrew, it will bring with a working version of readline.

  • brew install ruby

Then follow the instructions to add it to your PATH. Then execute gem install irb if it says can't find gem irb.

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 with rbenv

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
Ticktock answered 16/6, 2016 at 0:26 Comment(9)
Thanks Dave! Compiling now, will accept once I can verify. Incidentally, I had just voted up your question/answer on #37618019 as part of the research I was doing.Emulous
I wrote that one as a byproduct of investigating the irb problem. We've been on the same trail.Ticktock
Thanks, I had this exact issue on OS X when running rails console, with Ruby installed through rbenv. Furthermore, I would also get the error "Cannot read termcap database; using dumb terminal settings." This seemed to fix that as well.Marigolde
@DaveSchweisguth I already had readline installed via brew, tried reinstalling Ruby, but it's not working yet. I'm using Ruby 2.3.1 with rbenv. Is there any additional config I should add to ~/.irbrc?Arvid
To make it work I added IRB.conf[:SAVE_HISTORY] = 100000 to ~/.irbrc.Arvid
The answer should mention the need to add IRB.conf[:SAVE_HISTORY] = 100000 to ~/.irbrcAshlar
I think that not setting SAVE_HISTORY is a little different: if you forget that, history won't be written at all, rather than replacing history from the previous session.Ticktock
Worked great! Man, this had been bugging me for a long time. Thanks for the fix.Avens
Thanks Dave! your solution(Using Homebrew) worked for me.Transude

© 2022 - 2024 — McMap. All rights reserved.