Book cover- 'The Art of the Shim: Low-Alcohol Cocktails To Keep You Level'
Apparently, a shim is also a low-alcohol cocktail, especially one served at breakfast. Photo attribution here.

RBENV ensures that we're using the version of Ruby that our current environment depends on. It does this by:

  • intercepting your call to the ruby command,
  • deciding which Ruby version to use based on the information available to it, and then
  • passing the command you entered to the correct Ruby interpreter based on that version.

This behavior (intercepting a message, handling it in some way, and then passing it on to its intended receiver) is a common pattern in software. A file, program, or library which does this is sometimes known as a "shim".

Programs that act as shims are meant to be "transparent", meaning that you think you're talking to the program you invoked (say, Bundler if we're running bundle install), but the whole time you're really talking to the shim. Similarly, Bundler thinks it's responding to you, not to a shim.

How RBENV Behaves Like A Shim

To quote RBENV's README file:

“After RBENV injects itself into your $PATH at installation time, any invocation of ruby, gem, bundler, or other Ruby-related executable will first activate RBENV. Then, RBENV scans the current project directory for a file named .ruby-version.

If found, that file determines the version of Ruby that should be used within that directory. Finally, RBENV looks up that Ruby version among those installed under ~/.rbenv/versions/.”

So RBENV inserts itself in-between you and (for example) the ruby command. It does this by altering the $PATH environment variable, adding its own version of the ruby command in front of the canonical version. This way, it makes itself the recipient of your call to the ruby command. From there, it can do its job of selecting the correct version of Ruby, and passing your call to that version.

The file whose code we read in Part 1 of "The Impostor's Guide To The Shell" (~/.rbenv/shims/bundle) is the shim file for the Bundler gem's bundle command.

Photo Attribution

Title of Photo: Mimosa Drink on a Champagne Glass

Description: Mimosa Drink on a Champagne Glass

Author: Sabel Blanco

Source: Pexels

License: Free to use