- 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 API
- RUBYGEMS.ORG API
- RUBYGEMS.ORG API V2.0
- 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
- RESOURCES
- CONTRIBUTING TO RUBYGEMS
- FREQUENTLY ASKED QUESTIONS
- PLUGINS
- CREDITS
- COMMON VULNERABILITIES AND EXPOSURES
- RELEASING RUBYGEMS
- TRUSTED PUBLISHING
Use of common RubyGems commands
The gem
command allows you to interact with RubyGems.
Ruby 1.9 and newer ships with RubyGems built-in but you may need to upgrade for bug fixes or new features. To upgrade RubyGems, visit the download page.
If you want to see how to require files from a gem, skip ahead to What is a gem
- Finding Gems
- Installing Gems
- Requiring Code
- Listing Installed Gems
- Uninstalling Gems
- Viewing Documentation
- Fetching and Unpacking Gems
- Further Reading
FINDING GEMS
The search
command lets you find remote gems by name. You can use regular expression characters in your query:
$ gem search ^rails
*** REMOTE GEMS ***
rails (7.0.3)
rails-3-settings (0.1.1)
rails-acm (0.1.0)
rails-action-args (0.1.1)
rails-action-authorization (1.1.2)
[...]
If you see a gem you want more information on you can add the details option. You’ll want to do this with a small number of gems, though, as listing gems with details requires downloading more files:
$ gem search ^rails$ -d
*** REMOTE GEMS ***
rails (7.0.3)
Author: David Heinemeier Hansson
Homepage: https://rubyonrails.org
Full-stack web application framework.
You can also search for gems on rubygems.org such as this search for rake
INSTALLING GEMS
The install
command downloads and installs the gem and any necessary dependencies then builds documentation for the installed gems.
$ gem install drip
Fetching drip-0.1.1.gem
Fetching rbtree-0.4.5.gem
Building native extensions. This could take a while...
Successfully installed rbtree-0.4.5
Successfully installed drip-0.1.1
Parsing documentation for rbtree-0.4.5
Installing ri documentation for rbtree-0.4.5
Parsing documentation for drip-0.1.1
Installing ri documentation for drip-0.1.1
Done installing documentation for rbtree, drip after 0 seconds
2 gems installed
Here the drip command depends upon the rbtree gem which has an extension. Ruby installs the dependency rbtree and builds its extension, installs the drip gem, then builds documentation for the installed gems.
You can disable documentation generation using the --no-document
argument when installing gems.
REQUIRING CODE
RubyGems modifies your Ruby load path, which controls how your Ruby code is found by the require
statement. When you require
a gem, really you’re just placing that gem’s lib
directory onto your $LOAD_PATH
. Let’s try this out in irb
.
% irb
irb(main):001:0> pp $LOAD_PATH
[".../lib/ruby/site_ruby/3.1.0",
".../lib/ruby/site_ruby/3.1.0/x86_64-linux",
".../lib/ruby/site_ruby",
".../lib/ruby/vendor_ruby/3.1.0",
".../lib/ruby/vendor_ruby/3.1.0/x86_64-linux",
".../lib/ruby/vendor_ruby",
".../lib/ruby/3.1.0",
".../lib/ruby/3.1.0/x86_64-linux"]
By default you have just a few system directories on the load path and the Ruby standard libraries. To add the awesome_print directories to the load path, you can require one of its files:
$ gem install awesome_print
[...]
$ irb
irb(main):001:0> require "ap"
=> true
irb(main):002:0> pp $LOAD_PATH.first
".../gems/awesome_print-1.9.2/lib"
Tip: Passing -r
to irb
will automatically require a library when irb is loaded.
$ irb -rap
irb(main):001:0> ap $LOAD_PATH
[
[0] ".../bundle/gems/awesome_print-1.9.2/lib",
[1] ".../lib/ruby/site_ruby/3.1.0",
[2] ".../lib/ruby/site_ruby/3.1.0/x86_64-linux",
[3] ".../lib/ruby/site_ruby",
[4] ".../lib/ruby/vendor_ruby/3.1.0",
[5] ".../lib/ruby/vendor_ruby/3.1.0/x86_64-linux",
[6] ".../lib/ruby/vendor_ruby",
[7] ".../lib/ruby/3.1.0",
[8] ".../lib/ruby/3.1.0/x86_64-linux"
]
Once you’ve required ap
, RubyGems automatically places its lib
directory on the $LOAD_PATH
.
That’s basically it for what’s in a gem. Drop Ruby code into lib
, name a Ruby file the same as your gem (for the gem “freewill” the file should be freewill.rb
, see also name your gem) and it’s loadable by RubyGems.
The lib
directory itself normally contains only one .rb
file and a directory with the same name as the gem which contains the rest of the files.
For example:
% tree freewill/
freewill/
└── lib/
├── freewill/
│ ├── user.rb
│ ├── widget.rb
│ └── ...
└── freewill.rb
Leave a Reply