irb历史不起作用

时间:2010-01-14 17:01:01

标签: ruby irb

在〜/ .irbrc中我有这些行:

require 'irb/ext/save-history'
#History configuration
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

然而当我跑irb并点击向上箭头时没有任何反应。也没有创建指定的irb历史文件,也没有记录任何内容。

6 个答案:

答案 0 :(得分:20)

irb history在Debian Linux中开箱即用。没有etc / irbrc,也没有〜/ .irbrc。所以,嗯。

This person比你做了更多的事情。你认为ARGV.concat可能是缺失的部分吗?

require 'irb/completion'
require 'irb/ext/save-history'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history" 

答案 1 :(得分:11)

我没有给你答案为什么以上不起作用,但我确实在我的系统上找到了一个文件/etc/irbrc(OS X - Snow Leopard,Ruby 1.8.7)对我而言,这是一段有效的历史。所以有两条建议:i)检查你的/ etc / irbrc(或等效物)以确保其中没有任何可能会干扰你的设置的东西,以及ii)尝试下面的设置,看看你是否可以得到以这种方式工作的历史。

# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks

unless defined? ETC_IRBRC_LOADED

  # Require RubyGems by default.
  require 'rubygems'

  # Activate auto-completion.
  require 'irb/completion'

  # Use the simple prompt if possible.
  IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT

  # Setup permanent history.
  HISTFILE = "~/.irb_history"
  MAXHISTSIZE = 100
  begin
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
      Readline::HISTORY.push(*lines)
    else
      puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if $VERBOSE
  end

  ETC_IRBRC_LOADED=true
end

答案 2 :(得分:1)

这是一个已知错误,可以使用补丁。最简单的解决方案是覆盖save-history.rb:

/usr/lib/ruby/1.8/irb/ext/save-history.rb

固定版本:

http://pastie.org/513500

或一次性完成:

wget -O /usr/lib/ruby/1.8/irb/ext/save-history.rb http://pastie.org/pastes/513500/download

答案 3 :(得分:0)

检查以确保使用libreadline构建ruby,因为如果没有它,irb历史似乎无效。

答案 4 :(得分:0)

如果您有额外的irb配置文件,例如,也可能发生这种情况。 ~/.irbrc。如果是这种情况,请将liwp的答案中的内容复制到额外的配置中,它应该有效。

答案 5 :(得分:0)

我在ubuntu 20.04上遇到了同样的问题,并通过运行以下命令进行了修复:

gem install irb
相关问题