Last Updated: February 25, 2016
·
3.198K
· maksoy

Make your own gem

From start to finish, show you how to package your Ruby code in a gem
First we’re going to create a simple gem called hello that generates a hello world

$ bundle gem hello
     create  hello/Gemfile
      create  hello/Rakefile
      create  hello/.gitignore
      create  hello/hello.gemspec
      create  hello/lib/hello.rb
      create  hello/lib/hello/version.rb
Initializating git repo in /Users/mert/....

we’ll walk you through some of the generated files, starting with the hello.gemspec file.

# -*- encoding: utf-8 -*-  
$:.push File.expand_path("../lib", __FILE__)  
require "hello/version"  

Gem::Specification.new do |s|  
  s.name        = "hello"  
  s.version     = Hello::VERSION  
  s.platform    = Gem::Platform::RUBY  
  s.authors     = ["TODO: Write your name"]  
  s.email       = ["TODO: Write your email address"]  
  s.homepage    = ""  
  s.summary     = %q{TODO: Write a gem summary}  
  s.description = %q{TODO: Write a gem description}  

  s.rubyforge_project = "hello"  

  s.files         = `git ls-files`.split("\n")  
  s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")  
  s.executables   = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }  
  s.require_paths = ["lib"]  
end 

We can see straight away by looking at the TODO items that this file is designed to be edited directly. Once we complete these items we’ll have a working Gemspec file.

Now we are going to modify our hello file.

require "hello/version"
require 'hello/translator'

module Hello
  def self.hi(language = "english")
    translator = Translator.new(language)
    translator.hi       
  end
end

The Translator is now in lib/hello, which can easily be picked up with a require statement from lib/hello.rb. The code for the Translator:

class Hello::Translator
    def initialize(language)
        @language = language
    end

    def hi
        case @language
        when "spanish"
            "hola mundo"
        else
            "hello world"
        end
    end
end

Now you can build a gem from it. Then you can install the generated gem locally to test it out.

$ gem build hello.gemspec
    Successfully built RubyGem
    Name: hello
    Version: 0.0.1
    File: hello-0.0.1.gem

$ gem install ./hello-0.0.0.gem
    Successfully installed hello-0.0.1
    1 gem installed

Of course, the smoke test isn’t over yet: the final step is to require the gem and use it:

$ irb
    >>require 'hello'
    => true
    >>Hello.hi
    =>Hello world!
    >>Hello.hi("spanish")
    =>hola mundo

Bundler is a great solution for creating and managing gems.

https://github.com/yuzdeyuzmert/hello-gem

2 Responses
Add your response

Also, bundler includes a Rakefile with gem tasks for building and releasing your newly created gem: https://github.com/bundler/bundler/blob/master/lib/bundler/gem_helper.rb#L47-L50

over 1 year ago ·

The code in the Rakefile generates three tasks.
1) building, installing and releasing the gem with convenience tags
2) A common workflow to call rake install when a gem is set up the way we want so that we can install it on our local machine and test it out fully
3) and rake release tag that release and publish the gem to RubyGems.

over 1 year ago ·