1

When I'm using pry, the history of pry keeps overwriting my irb history and it is very annoying. If i'm using pry, i'd like to see pry history and if i'm using irb, i'd like to see irb history. Is there an obvious problem in my configuration?

~/.irbrc looks like

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:LOAD_MODULES] = [] unless IRB.conf.key?(:LOAD_MODULES)
unless IRB.conf[:LOAD_MODULES].include?('irb/completion')
  IRB.conf[:LOAD_MODULES] << 'irb/completion'
end
IRB.conf[:SAVE_HISTORY] = 1000

my .pryrc file is empty, which based on docs indicates to me that pry should be using .pry_history, which appears to be happening.

/etc/irbrc looks like

# 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
John
  • 1,246
  • 15
  • 34
  • What happens if you specify the history file location in the .pryrc file (example [here](https://github.com/pry/pry/issues/1418))? – Anssssss Dec 29 '15 at 21:05
  • I'm not certain that makes sense as I know for sure, .pryrc is working, I think the real question at hand is how to exclude commands that get written into the .pry_history from also getting written into the .irb_history file – John Dec 29 '15 at 21:34
  • I actually told pry explicitly to use .irb_history and that seems to work well, i can access whatever commands from wherever, which is nice so I think i'm going to stick with what I got. – John Dec 29 '15 at 21:43
  • 1
    Wait, what? Are you saying that the problem was pry was writing to BOTH irb_history and pry_history (and you didn't want it to clobber the irb_history)? But then you told pry to explicitly write to irb_history and now you're happy? – Anssssss Dec 29 '15 at 22:02
  • hmm, i don't know how to explain it, but I think the entire pry_history was overwriting irb history and destroying irb history, by setting pry to explicitly write to irb_history, now the entire irb_history isn't being overwritten, so basically, i have no idea but I'm happier with what I have :) – John Dec 29 '15 at 22:05
  • I've started to look into this myself. I believe the issue is that both IRB and Pry read your history from a file and push each line into `Readline`. Either there is no code telling Pry to remove and store IRB history before it loads its own history and to restore it upon exiting or there is and there's a bug somewhere. – Aaron Jul 22 '16 at 19:46

1 Answers1

0

Why the problem happens

Both Pry and IRB write their histories to Readline::HISTORY. When you enter Pry from IRB (or rails console) you have all your IRB history already in Readline::HISTORY. Pry then loads its history on top of that. When you exit Pry Readline::HISTORY isn't changed so you end up back in IRB with all of Pry's history appending to IRB's history. Finally, when you exit IRB it writes the history including Pry's to IRB's history file thus clobbering it.

Current state of the issue

I did some searching and found this is a known issue with Pry. I was also really interested in the problem so I managed to work up a solution that is awaiting a pull request review.

Workaround until merged

If you'd like to try it out before it get's merged you can grab the version from the branch I made on my fork of Pry. You can find it at https://github.com/agrberg/pry under branch save_irb_history_and_replace_on_close. If you use bundler all you need to do is add or update your line for pry to:

gem 'pry', git: 'git@github.com:agrberg/pry.git', branch: 'save_irb_history_and_replace_on_close'
Aaron
  • 13,349
  • 11
  • 66
  • 105