14

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.

Update

Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.

IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).

Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).

Hopefully this additional information will help you determine what you need to setup your system as you would like.

Pry Concerns

I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.

Community
  • 1
  • 1
Aaron
  • 13,349
  • 11
  • 66
  • 105

3 Answers3

9

I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.

IRB, and by extension, the Rails Console, read from ~/.irbrc, or if that doesn't exist, /etc/irbrc, to startup and configure irb. Your history is typically written to ~/.irb_history, but that is dictated by the contents of your irbrc file. The /etc/irbrc on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc that doesn't have history, or perhaps you have a syntax error in that file.

The debugger reads a file called .rdebugrc on startup. You can turn off history in debug by adding this line to ~/.rdebugrc:

set history save off

Turn it back on with:

set history save on

You could also set your debug output to go to a different file than irb reads from with the command:

set history filename

These also work from the debug prompt, but aren't persistent.

Ray Baxter
  • 3,181
  • 23
  • 27
  • Thanks for the answer Ray. I'm still trying to figure out what is going on. It could also be that I've been switching between Rails 2.3 and Rails 3 projects as well. I looked in my ~/.irbrc file and it doesn't mention anything about history. However /etc/irbrc says to use ~/.irb_history. Even stranger is that when I run irb or ./script/console I get my history (minus the first entry) written to ~/.irb_history AND ~/.irb-history. I deleted both to be sure and they were both created after one irb session. – Aaron Jun 29 '11 at 20:53
  • I created and put "set history ~/.rdebug_history" into ~/.rdebugrc. However, after going into the irb debugger from my rails app, I still don't have a ~/.rdebug_history file written though debugger does have history and it is different from irb's and script/console's. – Aaron Jun 29 '11 at 20:56
  • I noticed I had require 'utility-belt' in my ~/.irbrc. Commenting out this line made it so irb and script/console only produced ~/.irb-history. Putting it back in created and populated ~/.irb_history with all that was in ~/.irb-history. – Aaron Jun 29 '11 at 21:04
  • Yes, the way the these histories work is that they are read in on startup up, accumulated during your session, and then maxlines number of lines are written back out. If you start up one session of irb/console, perform some actions, and then start a different session in another window, that second session will begin with the same history as the first when it started. It won't have the commands that you entered in the first session. – Ray Baxter Jun 30 '11 at 03:47
  • Also, for debug, you can see the filename for where the debug is being stored by typing `show history` at the debug prompt. – Ray Baxter Jun 30 '11 at 03:51
  • Hey Ray, I don't know why it's working all of a sudden but this morning I tried `show history` in a debug session and it had the file I put in ~/.rdebugrc. When I exited, that file was actually written this time. Still not entirely sure why I have both ~/.irb-history and ~/.irb_history but it looks like irb history and debugger history aren't being confused anymore. – Aaron Jun 30 '11 at 14:01
  • Okay, one last revelation for now. In debugger, if I type `continue` instead of exit (and thus don't have to restart mongrel) the history doesn't get written. That at least explains why I wasn't getting anything written to ~/.rdebug_hist last night. – Aaron Jun 30 '11 at 14:04
  • Aaron, That's good infomation. I've been confused by that as well. I wonder if you do, continue, then stop at another break point, then exit, if you'll preserve the history from before the continue? – Ray Baxter Jun 30 '11 at 16:25
  • I just found out that, at leas on Linux Mint, the history file is actually called `~.irb-history` (so with a hyphen instead of an underscore) – Qqwy Mar 21 '16 at 11:12
  • 1
    @Qqwy I noticed this odd inconsistency too! I've updated my original question with my findings. tl;dr you are almost certainly using `rvm` which is why you have the hyphen instead of the underscore. – Aaron Jul 23 '16 at 15:12
4

There are a number of tools to help improve the irb experience. Bond and hirb are promising.

Here is Comprehensive list of Irb Tools and some tips on directly editing the .irbrc file.

Hope this help!

Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
twmills
  • 2,985
  • 22
  • 20
0

Although a very old question I got here by google. Turns out RVM slightly changed over time. Currently my IRB history (using rvm) is stored here:

user@host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history
Lumean
  • 1
  • 1