Ruby Gems

Contents

  • What is gem?
  • Commands
  • Gem from scratch
  • Build a gem
  • Use, Publish & install
  • make it executable
  • Documentation

Ruby Gems

  • Package manager framework
  • packaged code distributed using RubyGems

Gem Commands

  • Check if installed and version
  •  
  • gem list

 

gem -v
gem list

Gem from scratch

% tree
.
├── hola.gemspec
└── lib
    └── hola.rb

hola.rb

% cat lib/hola.rb
class Hola
  def self.hi
    puts "Hello world!"
  end
end

hola.gemspec

% cat hola.gemspec
Gem::Specification.new do |s|
  s.name        = 'hola'
  s.version     = '0.0.0'
  s.date        = '2010-04-28'
  s.summary     = "Hola!"
  s.description = "A simple hello world gem"
  s.authors     = ["Nick Quaranto"]
  s.email       = 'nick@quaran.to'
  s.files       = ["lib/hola.rb"]
  s.homepage    =
    'http://rubygems.org/gems/hola'
  s.license       = 'MIT'
end

Build it

% gem build hola.gemspec
Successfully built RubyGem
Name: hola
Version: 0.0.0
File: hola-0.0.0.gem

% gem install ./hola-0.0.0.gem
Successfully installed hola-0.0.0
1 gem installed

Use 

% irb
>> require 'hola'
=> true
>> Hola.hi
Hello world!

How to Publish?

$ curl -u qrush https://rubygems.org/api/v1/api_key.yaml >
~/.gem/credentials; chmod 0600 ~/.gem/credentials

Enter host password for user 'qrush':



% gem push hola-0.0.0.gem
Pushing gem to RubyGems.org...
Successfully registered gem: hola (0.0.0)

Install Gem

% gem list -r hola

*** REMOTE GEMS ***

hola (0.0.0)

% gem install hola
Successfully installed hola-0.0.0
1 gem installed

Add more files

% cat lib/hola/translator.rb
class Hola::Translator
  def initialize(language)
    @language = language
  end

  def hi
    case @language
    when "spanish"
      "hola mundo"
    else
      "hello world"
    end
  end
end
___________________________________________________________________________

% cat lib/hola.rb
require 'hola/translator'
class Hola
  def self.hi(language = "english")
    translator = Translator.new(language)
    translator.hi
  end
end

Update spec

% cat hola.gemspec
Gem::Specification.new do |s|
...
s.files       = ["lib/hola.rb", "lib/hola/translator.rb"]
...
end



Update spec

% cat hola.gemspec
Gem::Specification.new do |s|
...
s.files       = ["lib/hola.rb", "lib/hola/translator.rb"]
...
end



ADDING AN EXECUTABLE

% mkdir bin
% touch bin/hola
% chmod a+x bin/hola
_____________________________

% cat bin/hola
#!/usr/bin/env ruby

require 'hola'
puts Hola.hi(ARGV[0])
_____________________________

% ruby -Ilib ./bin/hola
hello world

% ruby -Ilib ./bin/hola spanish
hola mundo

Update Gemspec for bin

% head -4 hola.gemspec
Gem::Specification.new do |s|
  s.name        = 'hola'
  s.version     = '0.0.1'
  s.executables << 'hola'

Documentation

# The main Hola driver
class Hola
  # Say hi to the world!
  #
  # Example:
  #   >> Hola.hi("spanish")
  #   => hola mundo
  #
  # Arguments:
  #   language: (String)

  def self.hi(language = "english")
    translator = Translator.new(language)
    puts translator.hi
  end
end

Questions?

Thank You

Ruby Gems

By Datt Dongare

Ruby Gems

How to create a ruby gem.

  • 546