Your browser doesn’t support the features needed to display this presentation.

A simplified version of this presentation follows.

For the best experience, please use one of the following browsers:

Build Your Own RubyGems


Josh W Lewis @joshwlewis

Memphis Ruby Users Group

8/26/2013

RubyGems are:

Over 60,000 gems at Rubygems.org

Installing a gem is easy:

$ gem install bubs
$ bubs is really useful
ⓘⓢ ⓡⓔⓐⓛⓛⓨ ⓤⓢⓔⓕⓤⓛ

Don't reinvent the wheel

Check Ruby-Toolbox.com and RubyGems.org first.

Build a gem skeleton:

$ bundle gem elementy
    create   elementy/Gemfile
    create   elementy/Rakefile
    create   elementy/LICENSE.txt
    create   elementy/README.md
    create   elementy/.gitignore
    create   elementy/elementy.gemspec
    create   elementy/lib/elementy.rb
    create   elementy/lib/elementy/version.rb

Modify Gemspec

# elementy.gemspec
Gem::Specification.new do |spec|
  spec.name          = "elementy"
  spec.version       = Elementy::VERSION
  spec.authors       = ["Josh Lewis"]
  spec.email         = ["josh.w.lewis@gmail.com"]
  spec.description   = "Lookup Data for Elements in the Periodic Table"
  spec.files         = `git ls-files`.split($/)
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^test/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.3"
  spec.add_development_dependency "rake"
end

Add Data

- number: 1
  name: Hydrogen
  molar: 1.00794
  symbol: H
- number: 2
  name: Helium
  molar: 4.002602
  symbol: He

Add Code

# lib/elementy/element.rb
module Elementy
  class Element
    attr_accessor :number, :symbol, :name
    def self.search(term)
      if term =~ /\A\d*\Z/
        find { |e| e.number == term.to_i }
      else
        find { |e| [e.symbol, e.name].include? term.upcase }
      end
    end
  end
end

Add Tests

require 'test_helper'
describe Elementy::Element do
  it '::all must be an array' do
    Elementy::Element.all.must_be_instance_of Array
  end

  it 'must be Enumerable' do
    Elementy::Element.must_be_kind_of Enumerable
  end

  describe 'search' do
    it 'should find by number' do
      Elementy::Element.search(12).must_be_instance_of Elementy::Element
    end
  end
end

Add Documentation

    # Searches for and returns an element if found.
    #
    # @param term [string, symbol, integer] the name, 
    # symbol, or atomic number to search for
    # @return [Elementy::Element]
    def search(term)
      Elementy::Element.search term
    end

Add Executables

# bin/elementy
#!/usr/bin/env ruby
$:.unshift File.expand_path('../../lib', __FILE__)
require 'elementy'

if args.include?("--version") || args.include?("-v")
  p Elementy::VERSION
else
  if element = Elementy::Element.search(args.first)
    puts element.to_console
  else
    puts "Could not find element #{args.first}"
  end
end

Change Version

# lib/elementy/version.rb
module Elementy
  VERSION = "0.1.0"
end

Build Gem

$ gem build elementy.gemspec
  Successfully built RubyGem
  Name: elementy
  Version: 0.1.0
  File: elementy-0.1.0.gem

Push to Rubygems

$ gem push elementy-0.1.0.gem
Pushing gem to https://rubygems.org...
Successfully registered gem: elementy (0.1.0)

It's alive!

$ gem install elementy
Successfully installed elementy-0.1.0
1 gem installed
$ elementy 42
   symbol:      Mo
   name:        Molybdenum
   number:      42
   molar:       95.96
   position:    5
   group:       Element Transition d
   electrons:   [2, 8, 18, 13, 1]

More info