Rails quick tips #7: Project-specific .irbrc
- ruby rails productivity
In a previous installment of this series, I wrote about adding a project-specific .pryrc
to Ruby/Rails projects. However, over the past couple of years, IRB gained some nice features and I don’t use Pry much anymore because it’s one less dependency I need to worry about. That doesn’t mean I want to give up on per-project configuration files though, which turned out to be slightly more complicated with IRB.
Configuration file lookup
The way IRB and Pry load their configuration is significantly different. Pry looks for $XDG_CONFIG_HOME/pry/pryrc
or ~/.pryrc
first and then loads the .pryrc
file in the current directory if it exists (source). IRB will look for other configuration files only if ~/.irbrc
doesn’t exist and it will do so in the following order (source):
IRB reads from
~/.irbrc
when it’s invoked.If
~/.irbrc
doesn’t exist, irb will try to read in the following order:
.irbrc
irb.rc
_irbrc
$irbrc
This is a bit of a problem since I often work on more than one Ruby/Rails project and want to keep my general configuration in ~/.irbrc
while being able to override/augment it with an additional local file.
The solution
This is what I came up with to solve this problem.
- Add the
IRBRC
environment variable to the project’s.envrc
. I pretty much always usedirenv
, so this works well for me.export IRBRC="./.irbrc.local"
- Load the global
.irbrc
from the local file and add whatever else I need:load File.expand_path("~/.irbrc") # just an example def user @user ||= User.first end
- Add
.irbrc.local
and.irbrc.local_history
to either.gitignore
or~/.gitignore
(I did the latter).
While I do prefer Pry’s approach overall, this is easy enough to set up and configure.