Build your Ruby Gem

Tosh
3 min readDec 13, 2020

At first Ruby Gems seems magical . By running bundle install we could acquire these magic gems that would make do all sorts of things in our code.

the magic box

What it is ?

it is a package of open source Ruby code pre-written that you can download to use for your specific need without having to write anything !! If you are a gamer so you can compare these Gems to a DLC you download and giving you new functionality into your game. At the bare minimum, a gem includes a Ruby file and a gemspec. The gemspec (gem specification) describes the gem and is used by the RubyGems package manager to install the gem.

Create a Gem with bundler

1. Getting Started

To create a gem using Bundler, use the bundle gem command like this:

$ bundle gem best_gem

That’s it! We have created the best Gem!!

sorry… there is few more steps but promise it’s not complicated. After running bundle a folder will be created including all these:

create  best_gem/Gemfile
create best_gem/lib/best_gem.rb
create best_gem/lib/best_gem/version.rb
create best_gem/best_gem.gemspec
create best_gem/Rakefile
create best_gem/README.md
create best_gem/bin/console
create best_gem/bin/setup
create best_gem/.gitignore
create best_gem/.travis.yml
create best_gem/test/test_helper.rb
create best_gem/test/best_gem_test.rb

Then let go in our repository that bundle has created for us

$ cd best_gem

Open your file in your editor and go in lib/best_gem/best_gem.rb

3. Add some code

You will see a module named with the gem you have created, inside you can create a class or method to keep things simple, we will create a method who print a string :

lib/best_gem.rbmodule BestGem  def print
puts "Yatta my first gem"
end
end

4. Build install test !

Now that we 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 awesome_gem.gem locally:

$ gem build best_gemYou should get the following output:
Successfully built RubyGem
Name: best_gem
Version: 0.1.0
File: best_gem-0.1.0.gem

We might get some warning but because we are living on the edge we won’t care.

Ah Yeah! We just created a gem! We are going to be a bit selfish, we won’t share into the world and publish it on rubygems.org. Now that we have a gem file, we can install the gem on your computer.

$ gem install best_gemYou should get the following output:
Successfully installed best_gem-0.1.0
Parsing documentation for best_gem-0.1.0
Installing ri documentation for best_gem-0.1.0
Done installing documentation for best_gem after 0 seconds
1 gem installed

Test it with pry

$best_gem % pryin pry we require our gem
[1] pry(main)> require "best_gem"
=> true
then Include using the name of the module
[2] pry(main)> include BestGem
=> Object
and call the method we have created
[3] pry(main)> print
Yatta my first gem
=> nil

Most popular gems : https://rubygems.org/releases/popular

--

--