<< All versions
Using
Skill v1.0.0
currentAutomated scan0/100thebushidocollective/han/ruby-gems-and-bundler
──Details
PublishedMarch 25, 2026 at 01:15 PM
Content Hashsha256:e3b0c44298fc1c14...
Git SHAdiscovery:57
──Files
Files (1 file, 9.4 KB)
SKILL.md9.4 KBactive
SKILL.md · 527 lines · 9.4 KB
version: "1.0.0" name: ruby-gems-bundler user-invocable: false description: Use when working with Ruby gems, Bundler for dependency management, creating gemspecs, and publishing gems to RubyGems. allowed-tools:
- Bash
- Read
- Write
- Edit
Ruby Gems and Bundler
Master Ruby's package management system with gems and Bundler. Learn to manage dependencies, create gems, and publish to RubyGems.
Bundler Basics
Gemfile
ruby
source 'https://rubygems.org'# Ruby versionruby '3.3.0'# Production gemsgem 'rails', '~> 7.1'gem 'pg', '>= 1.1'gem 'puma', '~> 6.0'# Development and test gemsgroup :development, :test dogem 'rspec-rails'gem 'factory_bot_rails'gem 'faker'end# Development onlygroup :development dogem 'rubocop'gem 'rubocop-rails'end# Test onlygroup :test dogem 'capybara'gem 'selenium-webdriver'end# Git sourcegem 'my_gem', git: 'https://github.com/user/my_gem.git'# Local path (for development)gem 'local_gem', path: '../local_gem'# Specific branchgem 'experimental_gem', git: 'https://github.com/user/repo.git', branch: 'develop'# Require specific file or false to not auto-requiregem 'sidekiq', require: 'sidekiq/web'gem 'bootsnap', require: false
Version Constraints
ruby
# Exact versiongem 'rails', '7.1.0'# Pessimistic operator (allows patch updates)gem 'rails', '~> 7.1.0' # >= 7.1.0 and < 7.2.0gem 'rails', '~> 7.1' # >= 7.1.0 and < 8.0.0# Greater than or equalgem 'pg', '>= 1.1'# Rangegem 'ruby-version', '>= 1.0', '< 2.0'# Multiple constraintsgem 'nokogiri', '>= 1.10', '< 2.0'
Bundler Commands
bash
# Install all gems from Gemfilebundle install# Install to specific pathbundle install --path vendor/bundle# Update all gemsbundle update# Update specific gembundle update rails# Check for outdated gemsbundle outdated# Show gem locationbundle show rails# Execute command with bundled gemsbundle exec rspec# Open gem in editorbundle open rails# Check Gemfile syntaxbundle check# Remove unused gemsbundle clean# List all installed gemsbundle list# Show dependency treebundle viz
Gemfile.lock
The Gemfile.lock file locks gem versions for consistent installations:
ruby
# Always commit Gemfile.lock to version control# This ensures all developers use same gem versions# Regenerate Gemfile.lockrm Gemfile.lockbundle install
Creating Gems
Gem Structure
bash
# Create new gembundle gem my_gem# Structure:my_gem/├── lib/│ ├── my_gem/│ │ └── version.rb│ └── my_gem.rb├── spec/│ ├── my_gem_spec.rb│ └── spec_helper.rb├── bin/│ ├── console│ └── setup├── .gitignore├── Gemfile├── LICENSE.txt├── my_gem.gemspec├── Rakefile└── README.md
Gemspec
ruby
# my_gem.gemspecrequire_relative 'lib/my_gem/version'Gem::Specification.new do |spec|spec.name = "my_gem"spec.version = MyGem::VERSIONspec.authors = ["Your Name"]spec.email = ["your.email@example.com"]spec.summary = "Short summary of gem"spec.description = "Longer description of what gem does"spec.homepage = "https://github.com/username/my_gem"spec.license = "MIT"spec.required_ruby_version = ">= 3.0.0"spec.metadata["homepage_uri"] = spec.homepagespec.metadata["source_code_uri"] = "https://github.com/username/my_gem"spec.metadata["changelog_uri"] = "https://github.com/username/my_gem/CHANGELOG.md"# Specify which files should be added to the gemspec.files = Dir.chdir(File.expand_path(__dir__)) do`git ls-files -z`.split("\x0").reject do |f|f.match(%r{\A(?:test|spec|features)/})endendspec.bindir = "exe"spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }spec.require_paths = ["lib"]# Runtime dependenciesspec.add_dependency "activesupport", "~> 7.0"spec.add_dependency "nokogiri", ">= 1.10"# Development dependenciesspec.add_development_dependency "rspec", "~> 3.12"spec.add_development_dependency "rubocop", "~> 1.50"end
Version File
ruby
# lib/my_gem/version.rbmodule MyGemVERSION = "0.1.0"end
Main Library File
ruby
# lib/my_gem.rbrequire_relative "my_gem/version"require_relative "my_gem/core"require_relative "my_gem/helpers"module MyGemclass Error < StandardError; enddef self.configureyield configurationenddef self.configuration@configuration ||= Configuration.newendclass Configurationattr_accessor :api_key, :timeoutdef initialize@api_key = nil@timeout = 30endendend
Building and Publishing
Build Gem
bash
# Build gem locallygem build my_gem.gemspec# This creates my_gem-0.1.0.gem# Install locally for testinggem install ./my_gem-0.1.0.gem# Uninstallgem uninstall my_gem
Publish to RubyGems
bash
# First time setup (one-time)gem push my_gem-0.1.0.gem# You'll be prompted to log in# For subsequent pushesgem push my_gem-0.2.0.gem# Yank a version (removes from RubyGems)gem yank my_gem -v 0.1.0# Unyank a versiongem unyank my_gem -v 0.1.0
Versioning
ruby
# Semantic Versioning: MAJOR.MINOR.PATCH# 1.0.0 -> 1.0.1 (patch)# 1.0.1 -> 1.1.0 (minor)# 1.1.0 -> 2.0.0 (major)# lib/my_gem/version.rbmodule MyGemVERSION = "1.0.0"end# Update version, then build and push:# 1. Edit version.rb# 2. gem build my_gem.gemspec# 3. gem push my_gem-1.0.0.gem
RubyGems Commands
bash
# List installed gemsgem list# Search for gemsgem search rails# Show gem infogem info rails# List gem dependenciesgem dependency rails# Update all gemsgem update# Update specific gemgem update rails# Cleanup old versionsgem cleanup# Show gem environmentgem env# Install specific versiongem install rails -v 7.1.0# Install without documentation (faster)gem install rails --no-document# Uninstall gemgem uninstall rails# Fetch gem but don't installgem fetch rails
Gem Groups
ruby
# Define groupsgroup :development dogem 'pry'endgroup :test dogem 'rspec'endgroup :development, :test dogem 'factory_bot'end# Install without specific groupsbundle install --without production# Require specific groupsBundler.require(:default, :development)
Gem Sources
ruby
# Primary sourcesource 'https://rubygems.org'# Additional sourcessource 'https://gems.example.com' dogem 'private_gem'end# Git sourcesgem 'my_gem', git: 'https://github.com/user/my_gem.git'gem 'my_gem', git: 'https://github.com/user/my_gem.git', tag: 'v1.0'gem 'my_gem', git: 'https://github.com/user/my_gem.git', branch: 'main'gem 'my_gem', git: 'https://github.com/user/my_gem.git', ref: 'abc123'# GitHub shorthandgem 'my_gem', github: 'user/my_gem'# Local pathgem 'my_gem', path: '../my_gem'
Requiring Gems
ruby
# In coderequire 'my_gem'# Bundler auto-requires gems based on Gemfile# To disable auto-require:gem 'my_gem', require: false# Then manually require where needed:require 'my_gem'# Require specific filegem 'sidekiq', require: 'sidekiq/web'
Platform-Specific Gems
ruby
# Only install on specific platformsgem 'pg', platforms: :rubygem 'sqlite3', platforms: [:mingw, :mswin, :x64_mingw]# Multiple platformsplatforms :ruby dogem 'pg'gem 'nokogiri'endplatforms :jruby dogem 'activerecord-jdbc-adapter'end
Private Gems
Using Private Gem Server
ruby
# Gemfilesource 'https://rubygems.org'source 'https://gems.mycompany.com' dogem 'private_gem'end
Using Git Credentials
bash
# .netrc file for private reposmachine github.comlogin your-usernamepassword your-token
Gem Development
Using bundle console
bash
# Open IRB with gem loadedbin/console# Orbundle console
Running Tests
bash
# Using Rakerake spec# Or directlybundle exec rspec
Local Development
ruby
# In your app's Gemfile, point to local gemgem 'my_gem', path: '../my_gem'# Or use bundle configbundle config local.my_gem ../my_gem
Best Practices
- Always commit Gemfile.lock to version control
- Use pessimistic versioning (~>) for stability
- Keep gems updated but test thoroughly
- Use groups to separate dev/test/production gems
- Specify Ruby version in Gemfile for consistency
- Use bundle exec to ensure correct gem versions
- Document gem dependencies and why they're needed
- Test gems locally before publishing
- Follow semantic versioning for your gems
- Keep gemspecs clean and well-documented
Anti-Patterns
❌ Don't commit vendor/bundle to git (use .gitignore) ❌ Don't use `require: false` unnecessarily - adds manual work ❌ Don't specify exact versions unless absolutely necessary ❌ Don't push untested gem versions to RubyGems ❌ Don't include unnecessary files in gem packages ❌ Don't hardcode credentials in gemspec or Gemfile
Troubleshooting
bash
# Clear bundler cachebundle clean --force# Regenerate Gemfile.lockrm Gemfile.lock && bundle install# Check for gem conflictsbundle exec gem dependency# Verbose outputbundle install --verbose# Show why a gem is neededbundle show rails# List all gem versionsbundle list
Related Skills
- ruby-oop - For structuring gem code
- ruby-metaprogramming - Used in many gems
- ruby-standard-library - Core Ruby functionality