Package Management with
RUBYGEMS
What is RubyGems
• RubyGems is a package manager for the Ruby programming language that
provides a standard format for distributing Ruby programs and libraries (in a
self-contained format called a "gem"), a tool designed to easily manage the
installation of gems, and a server for distributing them.
• The RubyGems package manager can download and install gems to your system
and allows you to use the gems in other Ruby programs. Ruby 1.9 comes with
RubyGems installed by default.
• If you are using a version prior to Ruby 1.9, you can download RubyGems from
https://rubygems.org/pages/download.
• To use RubyGems in a pre Ruby 1.9 app, you will need to add this line in your
app: require 'rubygems'
• The interface for RubyGems is a command-line tool called gem which can install
and manage libraries (the gems).
• 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 to the latest RubyGems or install it for the first time type the following command on command
prompt
gem update --system
What is gem
Structure of a Gem
• Each gem has a name, version, and platform. For example, the rake gem has
a 0.8.7 version (from May, 2009). 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.
• 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.
• Use gem help platform command on command prompt for full details.
Each gem consists of:
• Code
• Documentation
• Gem specification (Gemspec)
• The code organization follows the following structure for a gem called gem_name:
gem_name/
├── bin/
│ └── gem_name
├── lib/
│ └── gem_name.rb
├── test/
│ └── test_gem_name.rb
├── README
├── Rakefile
└── gem_name.gemspec
• The lib directory contains the code for the gem.
• The test (or spec) directory is used for testing.
• Rakefile is used by Rake to automate tests and to generate code.
• README includes the documentation, RDOC, for most gems.
• Gem specification (gemspec) contains information about the author
of the gem, the time of creation and the purpose the gem serves.
1. Listing Installed Gems
• The list command shows your locally installed gems:
• E.g. gem list
2. Finding Gems
• The search command lets you find remote gems by name. You can use
regular expression characters in your query:
• E.g gem search csv
• 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:
• E.g.
3. Installing Gems
• The install command downloads and installs the gem and any
necessary dependencies then builds documentation for the installed
gems.
4. 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 and get some help from the pretty_print library
included with Ruby.
• Passing -r to irb will automatically require a library when irb is loaded.
• Once you’ve required gem (for e.x. require time), RubyGems
automatically places its lib directory on the $LOAD_PATH.
E.g.
5. Uninstalling Gems
• The uninstall command removes the gems you have installed.
6. Viewing Documentation
You can view the documentation for your installed gems with ri:
Creating a own Gem From Scratch
1. Create the basic file structure of the gem:
• Open command prompt and create the directories that will be
needed in your gem:
$ mkdir my_gem
$ cd my_gem
$ md lib
• That’s it! You need a root directory for your gem and a lib directory to
hold your Ruby file.
1. Create the basic file structure of the gem:
2. Create the gemspec
• Create a file named “mygem.gemspec” in your gem’s root directory. Then add
some code to make a valid gemspec:
mygem.gemspec
Gem::Specification.new do |s|
s.name = "MyGem"
s.version = "0.0.0"
s.date = "2021-03-19"
s.summary = "My First Gem Creation"
s.authors= ["abc","xyz"]
s.files = [
"lib/mygem.rb"
]
s.require_paths = ["lib"]
end
3.Add some code
• The file “mygem.rb” in the lib directory is the main file that will be used to
hold the Ruby code in this gem. /lib/mygem.rb
• The root file inside “lib” will usually match the name of the gem. In this
case “mygem” and “/lib/mygem.rb”.
mygem.rb
module IT
class SL
def self.Third
puts "You have Created First Gem!!"
end
end
end
4. Generate the gem file
• Now that you have some awesome code, you will want to
create a gem so you can use this awesome code in another
Ruby program.
• Rubygems has a command line interface that allows you to
create a gem.
• Fire off this command inside the root directory of your gem:
$ gem build mygem.gemspec
• This command will build the gem and output a gem file that will
include the version number in the file name.
• Since the gemspec contains a version attribute with a value of
“0.0.0”, the gem file will be named my_gem-0.0.0.
5. Install the gem
• Now that you have a gem file, you can use RubyGems to install the gem on your
computer.
• Typically you install gems from external sources, but you are not restricted to that.
• If you have access to the gem file, you can install it locally by specifying the
location of the gem file that will be installed.
• Here is the command to install mygem.gem locally:
$ gem install mygem-0.0.0.gem
6. Add The Gem to Another Ruby Program
• Create a new Ruby file that will be used to test out our gem.
• Let’s call it “sample.rb”.
• You can create this file anywhere on your system and then add the
following code so we can use the “SL?” class method from our gem.
• You just have to require ‘mygem’ and RubyGems will be able to find the
gem and make the class available to your program.
• Then you just call the class method from your namespaced class. Here is
the code:
sample.rb
require 'mygem’
IT::SL.Third
Creating Second Gem
My_gem.gemspec
Gem::Specification.new do |s|
s.name = "my_gem"
s.version = "0.0.0"
s.date = "2021-03-18"
s.summary = "my_gem is the best"
s.authors = [‘Maya Dhone', ‘V Veda Sahithi']
s.files = [
"lib/my_gem.rb"
]
s.require_paths = ["lib"]
end
my_gem.rb
module IT
class SL
def self.Third
puts "YOU ARE BEST!!"
end
end
end
sample.rb
require 'my_gem’
IT::SL.Third