Ruby on Rails ( RoR ) is one of the best framework to build a website with back-end support. Since we can easily create very complex web application quickly and without know any other programming language. We can also create a ruby library, that you can use later as a gem.
Create a Ruby Library
Reusable libraries in Ruby are called gems. This guide will walk you through creating a gem and using it in a different project.
Bundler can create a good gem skeleton for you.
bundle gem my_gem cd gem
It inserts some features that we don’t need however, so we can just remove those.
We don’t need licenses for code we’re not releasing as open source, so remove LICENSE.txt.
Roll Out provides tasks for packaging and releasing the gem, so remove the existing gem tasks. We’ll add Roll Out later.
subl Rakefile # remove require 'bundler/gem_tasks'
Finally, fill out your README and gemspec with what your gem will do.
subl README.md subl my_gem.gemspec
Your gemspec should be just the Gem::Specification creation and nothing else; Rubygems will internally eval the file. There’s a few lines that Bundler adds that should be removed as they are not necessary, including the usage of git. Use Dir instead – it’s in the Ruby standard library and won’t fork off a process. Do not include test/spec or other development artifacts the .gemspec file.
# coding: UTF-8 Gem::Specification.new do |spec| spec.name = 'my_gem' spec.version = MyGem::VERSION spec.authors = ['Your Name'] spec.email = [Your Email Address] spec.description = 'Something about gem.' spec.summary = 'Small summary.' spec.homepage = 'http://www.worklabs.org' spec.files = Dir['lib/**/*.rb', 'Gemfile', 'my_gem.gemspec', 'Rakefile', 'README.md'] spec.executables = Dir['my_gem_cli'] spec.test_files = Dir['spec/**/*.rb'] spec.require_paths = ['lib'] end
Place runtime dependencies using add_dependency in .gemspec file Do not use add_development_dependency, as they are not necessarily just development-only artifacts.
my_gem.gemspec # coding: UTF-8 Gem::Specification.new do |spec| spec.name = 'my_gem' … spec.add_dependency 'railties', '~> 4.0.0' spec.add_dependency 'actionpack', '~> 4.0.0' spec.add_dependency 'activesupport', '~> 4.0.0' end
Place build-time dependencies in Gemfile, without any grouping including rake, roll_out, rspec, mini-test, rdoc, rubocop, etc.
Place development-time dependencies in Gemfile, under a group named ‘development’, including pry, guard, sqlite etc. Gems in this group will not be installed in deployments.
Gemfile source 'https://rubygems.org' source 'http://repo.worklabs.org/rubygems/' gemspec gem 'roll_out', '~> 1.4' # build-time dependency gem 'rspec', '~> 2.12' # build-time dependency # development-time dependencies below group :development do List all dependencies here end
In your Gemfile, add rspec as a development dependency:
gem 'rspec', '~> 2.12'
Install gems for development:
You can now run tests with the rspec command. To make some tests to run, create a spec directory and a spec helper:
bundle exec rspec --init
And finally, create a lib directory and a spec:
mkdir spec/lib subl spec/lib/my_gem_spec.rb
If you want to play around with your gem as you’re developing it, you can easily drop into an irb session with the current bundle (not system) environment:
Add roll_out to your Gemfile next to rspec. Note the addition of a source pointing to our internal Rubygems repo. You can see more repositories on the Ecosystem page.
Gemfile source 'https://rubygems.org' source 'http://repo.worklabs.org/rubygems/'
gem 'roll_out', '~> 1.4' gem 'rspec', '~> 2.12'
Install gems for development:
Create a project.yml file in the project root.