Ruby : How to create a gem? : for beginner

What is a gem?

Gem is basically a library of reusable ruby codes or simply ruby plugins. One should be thinking of creating ruby gems for some of the reasons

  • Want to make generic solutions quickly achievable to public or within organization
  • Share newer functionality with others
  • Organize reusable codes in larger projects

Introduction to gem development

Gems has actually no size limitations, it can go to any extent. For beginners, a gem could be a small code library that adds some helper methods into the existing ruby classes such as String class. In this tutorial we will try to add some helper methods in String class so that this methods could be used in testing or in Models of Rails framework.

Note: be careful while giving name to your gem. Make sure no naming conflict arises by checking name you want to take in or github. Make sure name is in small letters and use underscores for multiple words. See the cart below for more details.

ruby_parser require 'ruby_parser' RubyParser
rdoc-data require 'rdoc/data' RDoc::Data
net-http-persistent require 'net/http/persistent' Net::HTTP::Persistent
net-http-digest_auth require 'net/http/digest_auth' Net::HTTP::DigestAuth

There are four ways to create a new gem

  1. Create the directory tree with required files in proper way (not recommended)
  2. Rubymine’s new ‘Ruby gem’ project
  3. Using Bundler in terminal
  4. Using available frameworks such as newgem, hoe, echoe, gemhub, bones

3. Using Bundler in terminal

$ bundle gem unique_gem_name
# Example: $ bundle gem faker_enhanced
 create faker_enhanced/Gemfile
 create faker_enhanced/Rakefile
 create faker_enhanced/LICENSE.txt
 create faker_enhanced/
 create faker_enhanced/.gitignore
 create faker_enhanced/faker_enhanced.gemspec
 create faker_enhanced/lib/faker_enhanced.rb
 create faker_enhanced/lib/faker_enhanced/version.rb

You get your git initialized already, readme files, license and gemspec file. Among them most important is to customize the .gemspec file. do |spec|          = "faker_enhanced"
  spec.version       = FakerEnhanced::VERSION
  spec.authors       = ["Shiva Bhusal"]         = [""]
  spec.summary       = %q{It embeds faking ability in String class.}
  spec.description   = %q{Any string fields or variables could now have faking ability.}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = `git ls-files -z`.split("\x0")
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"

Firstly you would like to test your codes. [ Testing gem –> tutorial for will be written later, keep watching].

As you described in the .gemspec file, all your code goes inside the ./lib directory. The file that is included by the bundler is unique_name.rb in ./lib directory.

Install gem in local machine

To install the gem, you need to write a line of command in the terminal. Before that you need to write the following command.

$ rake -T
rake build # Build faker_enhanced-0.0.1.gem into the pkg directory
rake install # Build and install faker_enhanced-0.0.1.gem into system gems
rake release # Create tag v0.0.1 and build and push faker_enhanced-0.0.1.gem to Rubygems

You can see predefined rake tasks to build, install and release the gem. You need to build your gem ( will create a .gem file containing all information of the gem). Then you would like to install the gem and then publish your gem to

Note: to release the gem you need to be logged in to rubygem. So follow the instructions to publish the gem.

  • goto
  • signup if new user
  • $ gem push unique_gem_name.gem
  • Then you will be asked you email address and password.
  • Then your gem will be published in the

To use the gem

gem 'fake_enhanced' # Add this line in the gemfile
$ gem install faker_enhanced

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s