All posts

How to read Ruby open source code locally

I’m sure TMTOWTDI here but I hope this helps you get started. The end goal here is to be able to run the following command in your terminal bundle open gem_name and have the code for the library opened in your text editor.

To start, use your bash, zsh, or other terminal shell to export a variable called BUNDLER_EDITOR. Bundler will use this environment variable to open the ruby gem library code in your text editor I’m using vscode and pass the appropriate command to open it code -a .. The full command on the terminal looks like:

export BUNDLER_EDITOR="code -a ."

From there navigate to the ruby project that is using the gem you want to read source code for, you will need to have the gem declared in your Gemfile for this to work. After the gem you want to read is included in a ruby project, navigate to the root directory of that project on your command line and run:

bundle open gem_name

A more explicit example:

Generate a new rails project: rails new test_project

Open the Gemfile and add the YAML gem to your dependencies:

gem "yaml"

Navigate to the project root directory:

cd test_project

Open the source code the for yaml gem:

bundle open yaml

After running the bundle open command the library code for YAML should be opened in your text editor.

Note that if you run the bundle open command without setting a value for BUNDLE_EDITOR you will be met with the following message:

“To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR“

So you could alternatively set a value for $EDITOR and you should have the same outcome. Dealers choice.

Further reading:


I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).