Gems against pip

Ruby: Gemfile, gemspec, lock

A Ruby project declares its dependencies in a Gemfile and describes the package itself in a gemspec. The lock file is the resolved answer, and it is committed.

source "https://rubygems.org" gem "minitest", "~> 5.20"

Gem::Specification.new do |spec| spec.name = "calc-demo" spec.version = "0.1.0" spec.summary = "A tiny calculator used by the comparison lessons" spec.authors = ["Arthur Isaev"] spec.files = ["lib/calc.rb"] spec.require_paths = ["lib"] end

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 lock file records the exact version that Bundler chose:

head -8 Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    minitest (5.27.0)

PLATFORMS
  ruby
  x86_64-linux-musl

Python: pyproject, venv, pip

A Python project declares the package in pyproject.toml and installs it into a virtual environment, which is the Python answer to a per-project gem folder.

[project] name = "calc-demo" version = "0.1.0" description = "A tiny calculator used by the comparison lessons" requires-python = ">=3.9" [build-system] requires = ["setuptools>=68"] build-backend = "setuptools.build_meta"

python3 -m venv .venv

.venv/bin/pip install -e .

Obtaining file:///tmp/vs/pkg
Successfully built calc-demo
Successfully installed calc-demo-0.1.0

-e is the editable install: the environment points at the source folder, so an edit is visible without reinstalling. The Ruby equivalent is bundle exec with the project on the load path, or gem install --local after a build.

.venv/bin/python -c 'import calc_demo; print(calc_demo.VERSION, calc_demo.add(2, 3))'

0.1.0 5

What differs

What to watch for

Commit the lock file of an application and leave it out of a library, in both ecosystems; the same rule, the same reason.

Do not install into the system Ruby or the system Python on a machine you share. A project folder and a lock file are enough to reproduce the environment, and the system stays clean.

Questions

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

  1. What do the Gemfile and the gemspec each describe?

    Answer

    The Gemfile lists the dependencies, the gemspec describes the package itself

    Python puts both into pyproject.toml, which is why the Ruby side has two files where the Python side has one.

  2. What does this program print? line = 'version = "0.1.0"' p line[/version = "([^"]+)"/, 1]

    Answer

    "0.1.0"

    The capture group reads the quoted value, which is how a script pulls one setting out of a TOML line.

  3. What does this program print? p File.join(".venv", "bin", "python")

    Answer

    ".venv/bin/python"

    The environment names the interpreter by a path, which is what a pipeline spells out.

  4. What does this program print? lock = "GEM\n remote: https://rubygems.org/\n specs:\n minitest (5.27.0)\n" p lock.lines.grep(/minitest/).size

    Answer

    1

    One line of the lock file names the gem and its resolved version.

  5. Where does a Python virtual environment install the packages?

    Answer

    Inside the environment folder, by default

    The environment is always local, while Ruby installs into the user gem folder unless bundle config sets a path.

  6. What does this program print? tables = ["[project]", "[build-system]"] p tables.first.delete("[]")

    Answer

    "project"

    delete removes every bracket character, and the first table of a pyproject is the one that names the package.

  7. Should a lock file be committed for a library?

    Answer

    No, it is a tool for applications; a library lets the consumer resolve the tree

    The rule is the same in both ecosystems: an application pins its tree, and a library leaves the choice to its consumer.

  8. What does this program print? p File.basename("Gemfile.lock")

    Answer

    "Gemfile.lock"

    The file has no folder part, so the base name is the whole name.

  9. What does this program print? files = ["Gemfile", "calc-demo.gemspec", "Gemfile.lock"] p files.select { |f| f.end_with?(".gemspec") }

    Answer

    ["calc-demo.gemspec"]

    The suffix names the package description, and the other two are dependency files.

  10. What does this program print? text = "GEM\nPLATFORMS\n" p text.lines.size

    Answer

    2

    Each line ends with a break, and the lock file has a section per heading.

Article author: Arthur Isaev