Skip to content

Instantly share code, notes, and snippets.

@Quemia
Last active April 2, 2020 03:04
Show Gist options
  • Select an option

  • Save Quemia/077051f422b9532cfbf5d6a11365fd6f to your computer and use it in GitHub Desktop.

Select an option

Save Quemia/077051f422b9532cfbf5d6a11365fd6f to your computer and use it in GitHub Desktop.

My First Gem

A gem is a library, a set of reusable Ruby files, labeled with a name and a version.

I

Create a basic file structure.

Ex:

         mkdir test_gem #pasta raiz   
         cd test_gem 
         mkdir lib 

II

Create a file with the extension .gemspec inside the root of your project.

Ex:

        "test_gem.gemspec"

III

Then add some specifications to create a valid gem.

Ex:

    Gem::Specification.new do |s|
        s.name        = 'test_gem'
        s.version     = '2.1.0'
        s.authors     = ["Quêmia"]
        s.email       = ["quemia@hotmail.com"]

        s.summary     = "This is an example!"
        s.description = %q[My first gem!]
        s.homepage    = "http://github.com"    
        s.license     = "MIT"

        s.files        = ["lib/test_gem.rb"]
    end

IV

Create a Ruby file inside the lib subfolder, this is the only file that must exist in the root of lib.  Add the code that the gem should have.

Ex:

  cat > test_gem.rb

  module AwesomeGem
    class WhoIs
      def self.awesome?
        puts "YOU ARE AWESOME!!"
      end
    end
  end

V

Generate the gem file. The command will create the gem and produce a gem file that will include the version number in the file name.

Ex:

  $ gem build awesome_gem.gemspec

    Successfully built RubyGem
    Name: test_gem
    Version: 2.1.0
    File: test_gem-2.01.0.gem

You should see the following output and some notices about missing attributes.

VI

Install the gem.

Ex:

gem install test_gem-2.01.0.gem

You should get the following output:

    Successfully installed awesome_gem-0.0.0
    1 gem installed
    Installing ri documentation for awesome_gem-0.0.0...
    Installing RDoc documentation for awesome_gem-0.0.0...

VII

Test the function:

Ex:

    % irb
    >> require 'test_gem.rb'
    => true
    >> AwesomeGem :: WhoIs.awesome?
    YOU ARE AWESOME!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment