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
~/.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
~/.irbrcwhen it’s invoked.
~/.irbrcdoesn’t exist, irb will try to read in the following order:
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.
This is what I came up with to solve this problem.
- Add the
IRBRCenvironment variable to the project’s
.envrc. I pretty much always use
direnv, so this works well for me.
- Load the global
.irbrcfrom the local file and add whatever else I need:
load File.expand_path("~/.irbrc") # just an example def user @user ||= User.first end
~/.gitignore(I did the latter).
While I do prefer Pry’s approach overall, this is easy enough to set up and configure.