Gems and Bundler
gem install
A gem is a packaged Ruby library with code and metadata. The gem command installs and lists these packages on your machine.
Check the tool itself first, then install one package by name to try it out quickly in a script.
gem -v
3.6.9
gem install minitest
Successfully installed minitest-6.0.6
1 gem installed
The Gemfile
The Gemfile lists the gems one project needs, with optional version limits. Bundler reads it to build a consistent set.
Keep this file in version control so every machine installs the same stack. The idioms from
Duck typing and core idioms
stay the same no matter which gems you add.
source "https://rubygems.org" gem "minitest", "~> 5.20"
Bundle install
bundle install reads the Gemfile and installs every missing gem. Run it after cloning a project and after editing the Gemfile.
Bundler resolves versions so all gems fit together, then remembers the result for the next section.
bundle -v
Bundler version 4.0.23
bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Bundle complete! 1 Gemfile dependency, 1 gem now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
The lockfile
Bundler writes exact versions into Gemfile.lock. This file freezes the whole tree, including dependencies of dependencies.
Commit the lockfile for apps so servers and teammates run identical code. Libraries usually leave it out because they support version ranges. More Ruby tooling is linked from the
Ruby overview.
GEM specs: minitest (5.27.0)
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the version limit
"~> 5.20"allow?Answer
5.20 and newer, but below 6.0
The pessimistic operator pins the last number it names and allows a newer minor version.
-
What does this program print?
req = Gem::Requirement.new("~> 5.20") puts req.satisfied_by?(Gem::Version.new("5.27.0")) puts req.satisfied_by?(Gem::Version.new("6.0.6"))Answer
true / false
5.27 is a newer 5.x, while 6.0.6 is outside the range the requirement allows.
-
What does
gem install minitestdo?Answer
Downloads and installs one gem on this machine
The command installs the named gem, while Bundler installs a whole Gemfile.
-
What does this program print?
req = Gem::Requirement.new("= 5.27.0") puts req.satisfied_by?(Gem::Version.new("5.27.1"))Answer
false
The equals sign asks for that exact version, and a patch release is not it.
-
What is the Gemfile for?
Answer
It lists the gems one project needs, with version limits
Bundler reads the Gemfile, and the resolved result goes into Gemfile.lock.
-
What does
bundle installdo?Answer
Reads the Gemfile and installs every missing gem it names
It resolves the versions so the gems fit together and installs what is missing.
-
What does Gemfile.lock hold?
Answer
The exact versions of the whole tree, dependencies included
The lockfile freezes the resolved tree, which is why every machine installs the same versions from it.
-
Why does an application commit Gemfile.lock while a library often does not?
Answer
An application needs identical versions everywhere, while a library supports a range of them
The lock is a deployment promise for an app, and a version range is the point of a library.
-
What does this program print?
puts Gem::Version.new("5.27.0") < Gem::Version.new("5.100.0") puts "5.27.0" < "5.100.0"Answer
true / false
Gem::Version compares the numbers, while a plain string compares character by character, so 27 loses to 100 but wins against the character 1.
-
Which line belongs at the top of a Gemfile?
Answer
source "https://rubygems.org"
The source line names the server the gems come from, and the gem lines follow it.
Article author: Arthur Isaev