Make your own gem.

·

3 min read

Make your own gem.

Ruby is a high-level, dynamic programming language. Ruby-1.9 or more comes with built-in Rubygems software. The Rubgems software is a package/library manager that manages other ruby libraries for your project, these packages are known as "gems" in the ruby world. Gems are used to enhance the capabilities of the existing ruby application. Rubygems software is packed into a command called "gem".

Let's make your first gem called "learn_to_code"

Create a file with the name learn_to_code.rb in a lib folder and another file with the name learn_to_code.gemspec in a working directory.

The code for the gem will be inside the lib folder, and the convention is that one file has a name similar to the gem so that it will get loaded when we call require "learn_to_code".

class LearnToCode
  def self.start
    puts "Happy Learning!!"
  end
end

Metadata for the gem goes into a gemspec file.

Gem::Specification.new do |s|
  s.name        = "learn_to_code"
  s.version     = "0.0.0"
  s.summary     = "Learn computer programing by using ruby language"
  s.description = "Learn computer programing!"
  s.authors     = ["Mukesh"]
  s.email       = "ashu.3333d@gmail.com"
  s.files       = ["lib/learn_to_code.rb"]
  s.homepage    = "https://rubygems.org/gems/learn_to_code"
  s.license       = "MIT"
end

Now to create a package run the command gem build learn_to_code.gemspec

To install the package locally run the command gem install ./learn_to_code-0.0.0.gem.

To test the gem, open irb and require the gem that we installed earlier.

Now to publish your gem you need to signup for an account at Rubegems.com.

Push your gem to Rubygem's repository by using the command gem push learn_to_code-0.0.0.gem and it will be available for everyone to use.

How to add more files

Add two classes ComputerArchtiecture and DesignPattern under into courses folder, and modify LearnToCode class that requires those classes.

class DesignPattern
  def self.start
    puts "Happy Design Pattern Learning!!"
  end
end
class ComputerArchtiecture
  def self.start
    puts "Happy Computer Archtiecture Learning!!"
  end
end
require "courses/computer_architecture"
require 'courses/design_pattern'

class LearnToCode
  def self.start(cource="Happy Learning!!")
    case cource
    when "Computer Archtiecture"
      ComputerArchtiecture.start
    when "Design Pattern"
      DesignPattern.start
    else
      puts "#{cource}"
    end
  end
end

Do not forget to update s.files with the paths of newly added classes.


Gem::Specification.new do |s|
  s.name        = "learn_to_code"
  s.version     = "0.0.1"
  s.summary     = "Learn computer programing by using Ruby language"
  s.description = "Learn computer programing!"
  s.authors     = ["Mukesh"]
  s.email       = "ashu.3333d@gmail.com"
  s.files       = ["lib/learn_to_code.rb", "lib/courses/computer_architecture.rb", "lib/courses/design_pattern.rb"]
  s.homepage    = "https://rubygems.org/gems/learn_to_code"
  s.license       = "MIT"
end

The naming convention for the gem

Gem namerequire statementclass name
Use underscores for multiple words e.g. learn_to_coderequire 'learn_to_code'LearnToCode
Use dashes to name a gem if it is inside the directory e.g. learn-to-coderequire 'learn/to/code'Learn::To::Code

Don't use uppercase letters to name a gem because Windows and OSX are case-independent but it will not work in other operating systems.