Run this command in your Terminal.
This creates and names all the files you need for your gem.
We are going to create a Lorem Ipsum Generator.
You can call it whatever you want but seeing as we are creating a Lorem Ipsum generator we'll call it lorem.
$ bundle gem lorem
cd into your lorem directory
$ cd lorem
Gemfile
lib
L lorem.rb
L lorem
L version.rb
LICENSE.txt
lorem.gemspec
Rakefile
README.md
\# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'lorem/version'
Gem::Specification.new do |spec|
spec.name = "lorem"
spec.version = Lorem::VERSION
spec.platform = Gem::Platform::RUBY
spec.authors = ["your name"]
spec.email = ["your@email.com"]
spec.homepage = ""
spec.summary = %q{Jenipsum generator}
spec.description = %q{Generates jenipsum text}
spec.license = "MIT"
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.files = `git ls-files`.split($/)
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
end
If you neglect to change these areas you will get this error when you go to run $ gem build lipsum.gemspec
lib/lorem.rb
require "lorem/version"
module Lorem def self.ipsum "Fantastic, yes, but gory. Help. It is a juniper. Pikku and Friendly ate all the kibbles. Formaldehyde, an exacto, a comb, a napkin, and a typewriter combined with vitamins, a bicycle, whiskey, batteries, flippers and a bike messenger twisted around the bend one curve at a time during the entire tour through Texas with Madi and my bass. " end end
require "thor"
require "lorem"
module Lorem
class CLI < Thor
desc "ipsum", "Lorem Ipsum text generator"
def ipsum
puts Lorem.ipsum
end
end
end
#!/usr/bin/env ruby
require 'lorem/cli'
Lorem::CLI.start
In the lorem.gemspec file, add
spec.add_runtime_dependency "thor"
before end.
\# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'lorem/version' Gem::Specification.new do |spec| spec.name = "lorem" spec.version = Lorem::VERSION spec.platform = Gem::Platform::RUBY spec.authors = ["your name"] spec.email = ["your@email.com"] spec.homepage = "" spec.summary = %q{Jenipsum generator} spec.description = %q{Generates jenipsum text} spec.license = "MIT" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" spec.add_runtime_dependency "thor"
spec.files = `git ls-files`.split($/) spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"] end
Gemfile
lib
L lorem.rb
L lorem
L version.rb
LICENSE.txt
lorem.gemspec
Rakefile
README.md
spec
L spec_helper.rb
In the lorem.gemspec file,
add
spec.add_development_dependency "rspec"
before end.
\# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'lorem/version' Gem::Specification.new do |spec| spec.name = "lorem" spec.version = Lorem::VERSION spec.platform = Gem::Platform::RUBY spec.authors = ["your name"] spec.email = ["your@email.com"] spec.homepage = "" spec.summary = %q{Jenipsum generator} spec.description = %q{Generates jenipsum text} spec.license = "MIT" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" spec.add_runtime_dependency "thor"
spec.add_development_dependency "rspec"
spec.files = `git ls-files`.split($/) spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"] end
Write this into your spec/lorem_spec.rb
require 'spec_helper' module Lorem describe Lorem do it "should not be empty" do expect(Lorem.ipsum).to_not be_empty end it "should include 'Fantastic'" do expect(Lorem.ipsum).to include('Fantastic') end end end
require "lorem"
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end
It is now time to run your test...
$ rspec spec/lorem_spec.rb
This is what you should see...
$ rspec spec/lorem_spec.rb
Run options: include {:focus=>true} All examples were filtered out; ignoring {:focus=>true} .. Finished in 0.0032 seconds 2 examples, 0 failures
If you see this, read the errors and check your code.
$ rspec spec/lorem_spec.rb Run options: include {:focus=>true} All examples were filtered out; ignoring {:focus=>true} FF Failures: 1) Lorem should not be empty Failure/Error: expect(Lorem.ipsum).to_not be_empty NoMethodError: undefined method `ipsum' for Lorem:Module # ./spec/lorem_spec.rb:6:in `block (2 levels) in <module:Lorem>' 2) Lorem should include 'Fantastic' Failure/Error: expect(Lorem.ipsum).to include('Fantastic') NoMethodError: undefined method `ipsum' for Lorem:Module # ./spec/lorem_spec.rb:10:in `block (2 levels) in <module:Lorem>'
Finished in 0.0032 seconds 2 examples, 2 failures
Once both your tests pass you
are finished and have a beautiful new gem to experiment with!
Thank you. I hope you enjoyed making a gem.
Checkout these reference where I gleaned a lot
of this info from.
http://railscasts.com/episodes/245-new-gem-with-bundler
http://bundler.io/v1.3/rubygems.html
http://guides.rubygems.org/make-your-own-gem
Also Thanks to Rails Girls Summer of Code where
I initially made this gem.
Thanks also to Jessica Lynn Suttles, Andre Arko and Joyce Hsu who all helped me with this presentation.