2

I use the following .irbrc with irb under ruby 2.3.5 on FreeBSD:

require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 10000
IRB.conf[:AUTO_INDENT]  = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:HISTORY_FILE] = "/home/ebot/.irb_history"

When I exit irb, the history gets saved to /home/ebot/.irb_history as expected. However, when I start another irb session, the Readline::History list is empty and pressing the <up> key has no effect.

I put the following code into my .irbrc:

  if File.exist?(IRB.conf[:HISTORY_FILE]) then
    prev = ''
    File.open(IRB.conf[:HISTORY_FILE]).each do |line|
      line.chomp!
      if line.length > 0 then
        if line != prev then
          puts "pushing hist <#{line}>"
          Readline::HISTORY.push(line)
          prev = line
        end
      end
    end
  end

When I execute irb with this, I see several lines of the above output, but still, history is empty:

% irb
irb(main):001:0> 1+1
=> 2
irb(main):002:0> 3+3
=> 6
irb(main):003:0> ^D

% cat .irb_history 
1+1
3+3

% irb
pushing hist <1+1>
pushing hist <3+3>
irb(main):001:0> Readline::HISTORY.to_a
=> ["Readline::HISTORY.to_a"]

So, irb seems to reset/empty this list after executing .irbrc.

How can I fix this?

Tom
  • 101
  • 1
  • 7
  • Use [`pry`](http://pryrepl.org/) instead of IRB, since it is much better in general and supports everything you might need, including history, out of the box. – Aleksei Matiushkin Aug 01 '18 at 10:46
  • Great, I've got the same problem with pry as well :( The only difference is, that `.pry_history` contains all commands ever entered. But `` still has no effect. Looks like an readline issue? – Tom Aug 01 '18 at 10:55
  • Try `Ctrl`+`R` and start typing the command that is presented in the history. Does it appear in the prompt line? – Aleksei Matiushkin Aug 01 '18 at 11:03
  • Nope. Seems to be an issue with ruby+readline. – Tom Aug 01 '18 at 11:06
  • 1
    Ok, my fault. I recompiled ruby. In the options menu I had libedit enabled instead of readline. I changed this, rebuilt, and now it works. – Tom Aug 01 '18 at 11:08
  • Cool then. My advice would be to still be using `pry`, though :) – Aleksei Matiushkin Aug 01 '18 at 11:11
  • See also [IRB history not working with Ruby 2.3.0](https://stackoverflow.com/questions/37847822/irb-history-not-working-with-ruby-2-3-0). – Franklin Yu Aug 23 '18 at 05:37

1 Answers1

3

On FreeBSD, ruby needs to be compiled with readline support, not libedit.

Tom
  • 101
  • 1
  • 7
  • This answer is also valid for Ruby that came with macOS. Installing any other Ruby (from Homebrew, or any Ruby version manager) solves this issue. – Franklin Yu Aug 23 '18 at 05:33