Sharing code between Ruby projects is simple when you package the code into gems. But keeping your local version synced to the development version can sometimes be a hassle.
While deploying updates as formal releases works for production, testing a gem locally with one of your projects requires a slightly different approach. Here’s an easy way to do it without creating unnecessary headaches:
One common way is to point your Gemfile to the local path of your gem:
gem "my_gem_name", path: "/user/my/projects/my_gem_name"
This replaces the version requirement (e.g., ~> 1.2) and lets Bundler use the local gem instead. However, be careful not to commit this change to avoid breaking your setup on other machines.
A better solution is to use Bundler’s configuration options to point to the local gem path:
bundle config local.my_gem_name ../projects/my_gem_name
bundle config --local local.my_gem_name ../projects/my_gem_name
If the git branch of your local gem version doesn’t match the Gemfile.lock, Bundler will throw a helpful error message. This ensures you catch potential issues early.
Now go forth and refactor with confidence, knowing your gems are synced and easy to manage!