tutorial on creating a custom Ruby Gem package _ruby topics

Source: Internet
Author: User

Write one of the simplest examples

1. Build the following folder

Note: The Lib directory must have a RB file that is the same as your gem name.

Copy Code code 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 a number of properties that can be set. I will write a special article to introduce.

The above field meaning, relatively simple. I believe we can all understand.


3. Compile and build Gems

  % Gem Build Hola.gemspec 
  successfully built Rubygem 
  Name:hola 
  version:0.0.0 
   
  % 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: In the version before Ruby 1.9.2, you need to require ' rubygem ' to 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 ': 


Release after setting

  % gem push Hola-0.0.0.gem 
  pushing gem to rubygems.org 
  ... Successfully registered Gem:hola (0.0.0) 


Publish successfully.

So any one can use the gem you wrote.


A slightly more complex example of Rubygem

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

Here's how to write multiple Ruby files.

1. Directory structure

A hola directory and translator.rb files.

  % 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 
   


Lib/hola.rb

  % Cat lib/hola.rb 
  class Hola 
   def self.hi (language =: 中文版) 
    translator = translator.new (language) 
    translator.hi 
   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 a different place from the top.


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


Finally, how to write a gem contains an example of an executable file.

This is also very simple. Like Rake is a typical gem that contains executable files.

1. Build a bin folder in just the engineering directory

The executable file is generated, and the Modify permission is runnable.

  % 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) 


Test under

  % ruby-ilib./bin/hola 
  Hello World 
   
  % ruby-ilib/bin/hola Spanish Hola 
   


3. Last modified Gemspec

  % head-4 hola.gemspec 
  gem::specification.new do |s| 
   S.name    = ' Hola ' 
   s.version   = ' 0.0.1 ' 
   s.executables << ' Hola ' 

The other 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.