- 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
~/.irbrcwhen it’s invoked.If
~/.irbrcdoesn’t exist, irb will try to read in the following order:
.irbrcirb.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
IRBRCenvironment variable to the project’s.envrc. I pretty much always usedirenv, so this works well for me.export IRBRC="./.irbrc.local" - 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 - Add
.irbrc.localand.irbrc.local_historyto either.gitignoreor~/.gitignore(I did the latter).
While I do prefer Pry’s approach overall, this is easy enough to set up and configure.
Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.