[Ruby Best practices] Best practices about Ruby bundle and Gemfile

Best practices about Ruby bundle and Gemfile

Modularity

It is not easy to maintain that Gemfile has too much content. Recoommand to modular Gemfile by split the single Gemfile file to mulitple Gemfile files.

First, split Gemfile content into mulitiple files prefix with Gemfile(such as Gemfile-base, Gemfile.base, etc.) according to your need, recommand by classification or usage.

1
2
3
4
# tree gemfiles
Gemfiles
├── Gemfile-base
└── Gemfile-logger

Then, replace Gemfile with these code:

1
2
3
4
5
6
7
8
9
10
# Gemfile
gemfiles = %w(Gemfiles/Gemfile-base Gemfiles/Gemfile-logger)
gemfiles.each do |gemfile|
if File.exist?(gemfile)
# or instance_eval File.read(gemfile)
eval_gemfile gemfile
else
puts("\e[31mWarning: #{gemfile} not found!\e[0m")
end
end

FAQs

Bundler: You must use Bundler 2 or greater with this lockfile

The issue was with gemfile.lock because our local bundle version and project bundle version was not matching!

You must upgrade gem ‘bundler’ to 2.2.8+.

1
$ gem update --system

References

[1] Rails Routing from the Outside In — Ruby on Rails Guides - https://guides.rubyonrails.org/routing.html

[2] Rails 5 Routing Cookbook: 10 recipes for the novice Rails developer and beyond | by Rui Freitas | Light the Fuse and Run | Medium- https://medium.com/lightthefuse/rails-5-routes-cookbook-10-recipes-for-the-novice-rails-developer-and-beyond-9986f43064bc

[3] ruby on rails - Bundler: You must use Bundler 2 or greater with this lockfile - Stack Overflow - https://stackoverflow.com/questions/53231667/bundler-you-must-use-bundler-2-or-greater-with-this-lockfile