Ruby gem

3 Min. Read
Sep 9, 2019

What is Ruby gem?

Ruby gem is a library/module which can be installed into a system running ruby platform. It is a package that specifies a single feature required for a system. Lets look into the structure of gem.

Structure of gem

A gem has its name, version and platform. Eg: slim-rails gem has version 3.2.0 - October 07, 2018 and its platform is ruby. So, it runs on any platform that Ruby runs.

Ruby gem has got 3 main components:

  • Code
  • Documentation
  • gemspec

A gem follows some standard structure of code organization:

1
2
3
4
5
6
7
8
9
10
11
% tree mygem
mygem/
|--bin/
  |--mygem
|--lib/
  |--mygem.rb
|--test/
  |--test_mygem.rb
|--README
|--Rakefile
|--mygem.gemspec
  • The bin directory contains an executable file which will be loaded into the user’s PATH when the gem is installed.
  • The lib directory contains the main file which contains the code of the gem.
  • The test directory contains test for the gem depending upon the test framework the developer uses.
  • Gem also has a README file which contains overall documentation of the gem. When the gem is installed documentation is automatically generated for the user.
  • There is also a Rakefile included which is used by the rake program to automate tests, generate code, and perform other tasks.
  • Finally, the gemspec file contains all the 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.

Example of gemspec file:

1
2
3
4
5
6
7
8
9
10
11
 % cat mygem.gemspec
Gem::Specification.new do |s|
  s.name        = 'mygem'
  s.version     = '1.0.0'
  s.summary     = "Mygem!"
  s.description = "My favourite gem Mygem!"
  s.authors     = ["Suman Tiwari"]
  s.email       = '[email protected]'
  s.homepage    = 'http://example.com/mygem'
  s.files       = ["lib/mygem.rb", ...]
end

Making your first gem

Let’s start creating a gem for yourself. At first create a .rb file with your gem name inside a lib directory. Also create a gemspec file that includes all information about author, version, platform etc as mentioned above. The convention is to have one Ruby file with the same name as your gem, since that gets loaded when require mygem is run. That one file is in charge of setting up your gem’s code and API.

The code inside lib/mygem.rb is everthing you want your gem to do. It contains some code that returns some output or performs some functionality.

1
2
3
4
5
6
7
cat mygem.rb
  class Mygem
    def self.welcome
      puts "Welcome to Mygem family."
    end
  end
end

After you have created a gemspec, you can build a gem from it. Then you can install the generated gem locally to test it out.

1
2
3
4
5
6
7
8
9
% gem build mygem.gemspec
Successfully built RubyGem
Name: mygem
Version: 0.0.0
File: mygem-0.0.0.gem

% gem install ./mygem-0.0.0.gem
Successfully installed mygem-0.0.0
1 gem installed

Now, open irb and use the gem.

1
2
3
4
5
irb
>> require 'mygem'
=> true
>> Mygem.hi
Welcome to Mygem family.

So, this is how you create a gem. In addition to that you need to write test cases for the functions defined in your gem inside the test directory.

Summary

Ruby gem is a simple ruby library that defines specific functionality. It is very easy to create and use ruby gem if you follow the structure of creating the gem. For any requirement of the user, he/she can easily create gem and share with the Ruby Community and allows others to use their creation. That’s all for today. Hope you enjoyed the blog.