Gems and Bundler

What are Gems?

Gems are Ruby libraries packaged for easy distribution and installation. They extend Ruby's functionality.

RubyGems

RubyGems is Ruby's package manager. It comes pre-installed with Ruby.

Basic Commands

# List installed gems
gem list

# Search for gems
gem search rails

# Install a gem
gem install rails

# Uninstall a gem
gem uninstall rails

# Update a gem
gem update rails

# Update all gems
gem update

# Get gem information
gem info rails

Gemfile

A Gemfile specifies project dependencies.

source 'https://rubygems.org'

gem 'rails', '7.0.0'
gem 'sqlite3'
gem 'puma', '>= 5.0'

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
end

group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
end

group :production do
  gem 'pg'
end

Bundler

Bundler manages gem dependencies and ensures everyone uses the same versions.

Installation

gem install bundler

Basic Commands

# Install all gems specified in Gemfile
bundle install

# Update gems to latest versions within constraints
bundle update

# Update specific gem
bundle update rails

# List all gems in bundle
bundle list

# Show gem information
bundle show rails

# Execute command in bundle context
bundle exec rails server

# Check for security vulnerabilities
bundle audit

Gem Versions

Version Constraints

# Exact version
gem 'rails', '7.0.0'

# Minimum version
gem 'rails', '>= 7.0.0'

# Version range
gem 'rails', '>= 7.0.0', '< 8.0.0'

# Pessimistic version constraint
gem 'rails', '~> 7.0'    # >= 7.0.0, < 8.0.0
gem 'rails', '~> 7.0.0'  # >= 7.0.0, < 7.1.0

Creating Gems

Basic Structure

# Create gem structure
bundle gem my_gem

# This creates:
# my_gem/
# ├── lib/
# │   └── my_gem.rb
# ├── my_gem.gemspec
# ├── Gemfile
# ├── Rakefile
# └── README.md

Gemspec File

Gem::Specification.new do |spec|
  spec.name          = "my_gem"
  spec.version       = MyGem::VERSION
  spec.authors       = ["Your Name"]
  spec.email         = ["your.email@example.com"]

  spec.summary       = "A short summary of your gem"
  spec.description   = "A longer description of your gem"
  spec.homepage      = "https://github.com/username/my_gem"
  spec.license       = "MIT"

  spec.files         = Dir.glob("lib/**/*")
  spec.require_paths = ["lib"]

  spec.add_dependency "rails", ">= 7.0.0"
  spec.add_development_dependency "rspec", "~> 3.0"
end

Building and Publishing

# Build gem
gem build my_gem.gemspec

# Install locally
gem install ./my_gem-1.0.0.gem

# Push to RubyGems
gem push my_gem-1.0.0.gem

Gem Development

lib/ Directory Structure

lib/
├── my_gem.rb
└── my_gem/
    ├── version.rb
    ├── core.rb
    └── utils.rb

Main File (lib/my_gem.rb)

require "my_gem/version"
require "my_gem/core"
require "my_gem/utils"

module MyGem
  # Main module code
end

Version File (lib/my_gem/version.rb)

module MyGem
  VERSION = "1.0.0"
end

Common Gems

Web Frameworks

Testing

Database

Utilities

Gem Best Practices

Naming

Versioning

Dependencies

Documentation

Testing

Troubleshooting

Gem Installation Issues

# Clear gem cache
gem cleanup

# Reinstall bundler
gem uninstall bundler
gem install bundler

# Use specific Ruby version
rvm use 3.2.0

Bundle Issues

# Clear bundle cache
bundle clean --force

# Reinstall all gems
rm -rf vendor/bundle
bundle install

# Check for conflicts
bundle check

Common Errors

Security

# Check for vulnerabilities
bundle audit

# Update vulnerable gems
bundle audit update

Gems are the lifeblood of the Ruby ecosystem. Understanding how to create, manage, and distribute gems is essential for Ruby development. Always keep your gems updated and be mindful of security vulnerabilities.

Loading