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.
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:
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"
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
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
 pry(main)> require "best_gem"
=> truethen Include using the name of the module
 pry(main)> include BestGem
=> Objectand call the method we have created
 pry(main)> print
Yatta my first gem
Most popular gems : https://rubygems.org/releases/popular