What is inside a gem

The layout

A gem is a folder with a gemspec and the files it lists. The convention is older than Bundler and every tool assumes it: the code in lib/, the command in bin/, one file in lib/ named after the gem:

demo ├── demo.gemspec ├── bin │ └── demo └── lib ├── demo.rb └── demo └── version.rb

require "demo" finds lib/demo.rb because lib is the require path. A second file lives in lib/demo/ and is loaded with require_relative from the first, which keeps the gem working when it is installed anywhere.

The gemspec

The gemspec is Ruby: a block that fills a Gem::Specification. Six fields are required and a few more make the gem publishable:

require_relative "lib/demo/version" Gem::Specification.new do |spec| spec.name = "demo" spec.version = Demo::VERSION spec.summary = "A demo gem" spec.authors = ["Ada"] spec.license = "MIT" spec.homepage = "https://example.com/demo" spec.files = ["lib/demo.rb", "lib/demo/version.rb", "bin/demo"] spec.bindir = "bin" spec.executables = ["demo"] spec.require_paths = ["lib"] spec.required_ruby_version = ">= 3.2" end

files is the package contents; a file that is not listed is not in the gem, which is the most common "works here, missing there" bug. executables and bindir are what put the command on PATH after install. license and homepage are not required, and gem build warns without them.

gem build

The build reads the gemspec and writes a .gem file named from the name and the version:

gem build demo.gemspec

Successfully built RubyGem Name: demo Version: 0.1.0 File: demo-0.1.0.gem

The version comes from the constant the gemspec required, so a release is a change to lib/demo/version.rb and a rebuild. A tag that does not match the version is the next thing a release pipeline checks.

Install and run

Installing the file is the same as installing a published gem: RubyGems unpacks it, links the executable and records it in the gem list.

gem install ./demo-0.1.0.gem

Successfully installed demo-0.1.0 1 gem installed

demo

demo 0.1.0

The command works from any folder because the binstub is on PATH; the code is loaded from the installed gem, not from the working directory. That is the difference between ruby bin/demo and demo, and the reason a packaging bug shows up only after install.

What to watch for

A hand-written files list drifts from the folder. Use Dir.glob in the gemspec, or check the packaged file list in CI, so a new file is packaged without an edit.

A gem that depends on another gem declares it in add_dependency, not in the README. A missing dependency installs cleanly and fails at the first require, which is the hardest failure to trace back to packaging.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? spec = Gem::Specification.new do |s| s.name = "demo" s.version = "0.1.0" s.summary = "A demo gem" s.authors = ["Ada"] s.files = ["lib/demo.rb"] end puts spec.full_name

    Answer

    demo-0.1.0

    full_name joins the name and the version with a dash, the same shape the built file uses.

  2. What does this program print? spec = Gem::Specification.new do |s| s.name = "demo" s.version = "0.1.0" s.summary = "A demo gem" s.authors = ["Ada"] s.files = ["lib/demo.rb"] end puts spec.files.include?("bin/demo")

    Answer

    false

    The files array is the package contents, so a file it does not name is not in the gem.

  3. Which folder is the require path of a gem?

    Answer

    lib/

    lib is on the require path, which is why require "demo" finds lib/demo.rb.

  4. What does spec.executables = ["demo"] with spec.bindir = "bin" do after install?

    Answer

    Puts a command named demo on PATH

    The executable and its folder are what make the installed command available from any directory.

  5. What does a hand-written files list most often cause?

    Answer

    A gem that works in the source folder and fails after install

    A file that is not listed is not packaged, which is the classic works-here, missing-there bug.

  6. Where does the version in the built gem file name come from?

    Answer

    The version constant the gemspec requires

    The gemspec reads the constant, which is why a release edits version.rb and rebuilds.

  7. What does gem install ./demo-0.1.0.gem do?

    Answer

    Unpacks the built file, links the command and records it in the gem list

    Installing a local file follows the same path as installing a published gem.

  8. How does a gem declare that it needs another gem?

    Answer

    With spec.add_dependency in the gemspec

    The dependency belongs in the gemspec; a missing one installs cleanly and fails at the first require.

  9. What does a Gem::Specification block fill?

    Answer

    The metadata and file list of one gem

    The gemspec is the description of the package, and gem build turns it into a .gem file.

  10. Why does the gemspec use require_relative for the version file?

    Answer

    To load the version from the gem folder before it is installed anywhere

    The relative load reads the file next to the gemspec, whatever the working directory is.

Article author: Arthur Isaev