71

I'm using the console in Ruby on Rails 3.1.1, and I'm finding its command history (up arrow) to be really flaky. I've run the commands p = Product.by_name 'Acme' and p.add_search_term('Snipe') several times today, across several console sessions.

Yet, when I reload the Ruby on Rails console, only the first one shows in my command history, not the second. Sometimes they are both there in the history after I reload the console. On top of that, I see commands in my history that are from several days ago after pressing up arrow only a few times. Is there some sort of configuration that I need to change or is this a bug?

  • Ruby on Rails 3.1.1`
  • Ruby 1.9.2p290`
  • Ubuntu 11.10`
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jcollum
  • 43,623
  • 55
  • 191
  • 321
  • 1
    Is the problem only with multiple consoles? That would certainly result in unexpected history. – Andrew Marshall May 06 '12 at 02:24
  • @AndrewMarshall nope, it's always a dev console that I'm opening in the same window with the same shell script – jcollum May 09 '12 at 23:59
  • Is the problem Rails specific, or do you get the same on a plain irb? Related question for no-Rails: http://stackoverflow.com/questions/2065923/irb-history-not-working – Ciro Santilli OurBigBook.com Oct 14 '14 at 16:54
  • Ciro that was a LONG time ago. I don't have this setup anymore and I don't work in Rails anymore. I can't answer your question. – jcollum Oct 14 '14 at 19:09
  • 1
    See also https://stackoverflow.com/questions/37847822/irb-history-not-working-with-ruby-2-3-0?noredirect=1&lq=1 which discusses how ruby must be complied with `readline` – Jared Beck Jan 31 '18 at 17:04

1 Answers1

161

From How To – Persist Rails or IRB Console Command History After Exit.

Create, or edit your ~/.irbrc file to include:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
  • 7
    I wish you'd put the solution (all 3 lines of it) in the body of the answer, in case the link ever goes away. – Ovesh Feb 18 '13 at 05:33
  • 7
    Note that RVM has a built-in irbrc that adds history for us: that's why it was working on my local machine but not on the server even without a `~/.irbrc`. – Ciro Santilli OurBigBook.com Sep 14 '14 at 10:59
  • Also, make sure you install Readline. See this post: https://stackoverflow.com/questions/37847822/irb-history-not-working-with-ruby-2-3-0 – Ryan Francis Jul 23 '18 at 19:15