- RubyGems Basics
- What is a gem?
- Make your own gem
- Gems with Extensions
- Name your gem
- Publishing your gem
- Security Practices
- Managing owners using UI
- Removing a Published gem
- SSL Certificate Update
- Patterns
- Specification Reference
- Command Reference
- RubyGems.org API
- RubyGems.org API V2.0
- RubyGems.org Compact Index API
- RubyGems.org rate limits
- API key scopes
- Run your own gem server
- Setting up multi-factor authentication
- Using multi-factor authentication in command line
- MFA requirement opt in
- Using S3 as gem source
- Default gems and bundled gems
- Resources
- Contributing to RubyGems
- Frequently Asked Questions
- Plugins
- Common Vulnerabilities and Exposures
- Trusted Publishing
- Organizations
- Credits
Bundler
- Bundler in gems
- Gemfiles
- Getting Started
- How to Upgrade to Bundler 2
- How to deploy bundled applications
- How to install gems from git repositories
- How to manage application dependencies with Bundler
- How to manage groups of gems
- How to manage dependencies with Bundler
- How to troubleshoot RubyGems and Bundler TLS/SSL Issues
- How to update gems with Bundler
- How to use Bundler in a single-file Ruby script
- How to use Bundler with Docker
- How to use Bundler with Rails
- How to use Bundler with Ruby
- How to use Bundler with RubyMotion
- How to use Bundler with Sinatra
- How to use git bisect with Bundler
- How to write a Bundler plugin
- Known Plugins
- Recommended Workflow with Version Control
- Ruby Directive
What is a gem?
Unpack the mystery behind what’s in a RubyGem.
Structure of a Gem
Each gem has a name, version, and platform. For example, the
rake gem has a 13.0.6 version (from Jul
2021). Rake’s platform is ruby, which means it works on any platform Ruby
runs on.
Platforms are based on the CPU architecture, operating system type and
sometimes the operating system version. Examples include “x86-mingw32” or
“java”. The platform indicates the gem only works with a ruby built for the
same platform. RubyGems will automatically download the correct version for
your platform. See gem help platform for full details.
Inside gems are the following components:
- Code (including tests and supporting utilities)
- Documentation
- gemspec
Each gem follows the same standard structure of code organization:
% tree freewill
freewill/
├── bin/
│ └── freewill
├── lib/
│ └── freewill.rb
├── test/
│ └── test_freewill.rb
├── README
├── Rakefile
└── freewill.gemspec
Here, you can see the major components of a gem:
- The
libdirectory contains the code for the gem - The
testorspecdirectory contains tests, depending on which test framework the developer uses - A gem usually has a
Rakefile, which the rake program uses to automate tests, generate code, and perform other tasks. - This gem also includes an executable file in the
bindirectory, which will be loaded into the user’sPATHwhen the gem is installed. - Documentation is usually included in the
READMEand inline with the code. When you install a gem, documentation is generated automatically for you. Most gems include RDoc documentation, but some use YARD docs instead. - The final piece is the gemspec, which contains information about the gem. The gem’s files, test information, platform, version number and more are all laid out here along with the author’s email and name.
More information on the gemspec file
The Gemspec
The gemspec specifies the information about a gem such as its name, version, description, authors and homepage.
Here’s an example of a gemspec file. You can learn more in how to make a gem.
% cat freewill.gemspec
Gem::Specification.new do |s|
s.name = 'freewill'
s.version = '1.0.0'
s.summary = "Freewill!"
s.description = "I will choose Freewill!"
s.authors = ["Nick Quaranto"]
s.email = 'nick@quaran.to'
s.homepage = 'http://example.com/freewill'
s.files = ["lib/freewill.rb", ...]
end
For more information on the gemspec, please check out the full Specification Reference which goes over each metadata field in detail.
Credits
This guide was adapted from Gonçalo Silva’s original tutorial on docs.rubygems.org and from Gem Sawyer, Modern Day Ruby Warrior.