Tutorial on creating a custom Ruby gem package, rubygem

Source: Internet
Author: User

Tutorial for creating a custom Ruby gem package, rubygem

Write a simple example

1. Create the following folders

Note: There must be an rb file in the lib directory with the same name as your gem.

Copy the code The code is as follows:
    $ cd hola
    $ tree
    .
    ├── hola.gemspec
    └── lib
        └── hola.rb


2. Write code

. 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'
  end

There are many properties that can be set. I will write an introduction specifically.

The meaning of the above field is relatively simple. I believe everyone can understand.


3. Compile and generate the gem

  % 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


4. Test use

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

Note: Before the ruby 1.9.2 version, you need to require 'rubygem' before you can use the gem we wrote.


5. Publish to rubygems website

  $ curl -u tom https://rubygems.org/api/v1/api_key.yaml>
  ~ / .gem / credentials
  Enter host password for user 'tom':

Post after setting

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

Published successfully.

In this way, anyone can use the gem you wrote.


Slightly complex rubygem example

The above example has only one ruby file, and the general gem should not be so simple.

Here's how to write multiple ruby files.

1. Directory structure

An additional hola directory and translator.rb file

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

2. Code

lib / hola / translator.rb

  % 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

lib / hola.rb

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

.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", "lib / hola / translator.rb"]
   s.homepage =
    'http://rubygems.org/gems/hola'
  end

Red is different from the above.


The other steps are the same as above. It's simple!


Finally, let me talk about how to write an example of a gem containing executable files.

This is also very simple. Like rake is a typical gem containing executable files.

1. Build a bin folder under the project directory just now

Generate an executable file, and modify the permissions to run.

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


2. Modify the contents of the executable file
bin / hola

  #! / usr / bin / env ruby
   
  require 'hola'
  puts Hola.hi (ARGV [0])


Under test

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


3. Finally modify the gemspec

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

The rest is the same as above. It's very simple.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.