irb历史记录已保存但未加载

时间:2018-08-01 10:43:41

标签: ruby readline irb

我在FreeBSD的ruby 2.3.5下使用以下.irbrc和irb:

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"

退出irb时,历史记录将按预期保存到/home/ebot/.irb_history。但是,当我启动另一个irb会话时,Readline::History列表为空,按<up>键无效。

我将以下代码放入了.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

当我用它执行irb时,我看到上面输出的几行,但是历史记录仍然是空的:

% 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"]

因此,irb在执行.irbrc之后似乎会重置/清空此列表。

我该如何解决?

1 个答案:

答案 0 :(得分:3)

在FreeBSD上,ruby需要使用readline支持而不是libedit进行编译。