How to create your own Ruby gem package

Source: Internet
Author: User

Write a simple example

1. Create the following folder

Note: The lib directory must have a Rb file with the same name as your gem.

[Ruby]View plaincopy
  1. $ CD Hola
  2. $ Tree
  3. .
  4. ├ ── Hola. gemspec
  5. ── Lib
  6. └ ── Hola. Rb


2. write code

. Hola. Rb

[Ruby]View plaincopy
  1. % Cat lib/Hola. Rb
  2. Class Hola
  3. Def self. Hi
  4. Puts "Hello world! "
  5. End
  6. End


. Hola. gemspec

 

[Ruby]View plaincopy
  1. % Cat Hola. gemspec
  2. GEM: specification. New do | S |
  3. S. Name = 'hola'
  4. S. Version = '0. 0.0'
  5. S. Date = '2017-04-28'
  6. S. Summary = "Hola! "
  7. S. Description = "a simple hello world gem"
  8. S. Authors = ["Nick quaranto"]
  9. S. Email = '[email protected]'
  10. S. Files = ["lib/Hola. RB"]
  11. S. Homepage =
  12. 'Http: // rubygems.org/gems/hola'
  13. End

Many attributes can be set here.

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


3. compile and generate the gem

[Ruby]View plaincopy
  1. % Gem build Hola. gemspec
  2. Successfully built rubygem
  3. Name: Hola
  4. Version: 0.0.0
  5. File: hola-0.0.0.gem
  6. % Gem install./hola-0.0.0.gem
  7. Successfully installed hola-0.0.0
  8. 1 gem installed


4. Test and use

[Ruby]View plaincopy
  1. % IRB
  2. > Require 'hola'
  3. => True
  4. > Hola. Hi
  5. Hello world!

Note: Before Ruby 1.9.2, you must first require 'rubygem 'to use the gem we wrote.


5. Publish to the rubygems website

[Ruby]View plaincopy
  1. $ Curl-u Tom https://rubygems.org/api/v1/api_key.yaml>
  2. ~ /. Gem/Credentials
  3. Enter host password for user 'Tom ':

Publish after setting

[Ruby]View plaincopy
  1. % Gem push hola-0.0.0.gem
  2. Pushing gem to rubygems.org...
  3. Successfully registered GEM: Hola (0.0.0)

Released successfully.

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


A slightly complex example of rubygem

The above example only contains a ruby file. Generally, the gem should not be so simple.

The following describes how to write multiple Ruby files.

1. directory structure

Added the hola directory and the translator. RB file.

[Ruby]View plaincopy
  1. % Tree
  2. .
  3. ├ ── Hola. gemspec
  4. ── Lib
  5. ├ ── Hola
  6. │ ── Translator. Rb
  7. └ ── Hola. Rb

2. Code

 

lib/hola/translator.rb
[Ruby]View plaincopy
  1. % Cat lib/Hola/translator. Rb
  2. Class Hola: Translator
  3. Def initialize (language)
  4. @ Language = Language
  5. End
  6. Def hi
  7. Case @ Language
  8. When: Spanish
  9. "Hola mundo"
  10. Else
  11. "Hello World"
  12. End
  13. End
  14. End
lib/hola.rb
[Ruby]View plaincopy
  1. % Cat lib/Hola. Rb
  2. Class Hola
  3. Def self. Hi (Language =: English)
  4. Translator = translator. New (language)
  5. Translator. Hi
  6. End
  7. End
  8. Require 'hola/translator'

. Hola. gemspec

[Ruby]View plaincopy
  1. % Cat Hola. gemspec
  2. GEM: specification. New do | S |
  3. S. Name = 'hola'
  4. S. Version = '0. 0.0'
  5. S. Date = '2017-04-28'
  6. S. Summary = "Hola! "
  7. S. Description = "a simple hello world gem"
  8. S. Authors = ["Nick quaranto"]
  9. S. Email = '[email protected]'
  10. S. Files = ["lib/Hola. RB", "lib/Hola/translator. RB"]
  11. S. Homepage =
  12. 'Http: // rubygems.org/gems/hola'
  13. End

The other steps are the same as above. Easy!

Finally, Let's explain how to write an example where the gem contains executable files.

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

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

Generate an executable file and modify the permission to run.

[Ruby]View plaincopy
  1. % Mkdir Bin
  2. % Touch bin/Hola
  3. % Chmod A + x bin/Hola


2. Modify the executable file content
Bin/Hola

[Ruby]View plaincopy
  1. #! /Usr/bin/ENV Ruby
  2. Require 'hola'
  3. Puts Hola. Hi (argv [0])


Test

[Ruby]View plaincopy
  1. % Ruby-ilib./bin/Hola
  2. Hello World
  3. % Ruby-ilib./bin/Hola Spanish
  4. Hola Mundo


3. Modify the gemspec

[Ruby]View plaincopy
  1. % Head-4 Hola. gemspec
  2. GEM: specification. New do | S |
  3. S. Name = 'hola'
  4. S. Version = '0. 0.1'
  5. S.exe cutables <'hola'

Others are the same as above. It's easy.

------------------------------------------------------------------------------

What is rubygems?

Rubygems is a convenient and powerful Ruby Package Manager. Ruby third-party plug-ins are managed using the gem method, which is very easy to publish and share, A simple command can be used to install third-party extension libraries. Features: Remote Installation Package, management of dependencies between packages, simple and reliable uninstallation, query mechanism, ability to query package information of local and remote servers, and ability to maintain different versions of a package, you can view information about the installed gem through the web-based view interface.

Install rubygems

Official site http://rubygems.org/pages/download

: Http://rubyforge.org/frs? Group_id = 126

Windows users directly download the. Zip package, decompress it, enter the directory of setup. Rb in the CMD Prompt window, and run Ruby setup. RB to install the package.

For Versions later than ruby1.9.1, you can directly enter the command gem Update-system in the CMD window to upgrade to the latest version.

Installation Method of the GEM package:

Rubygems.org is the official gem hosting center, where rubygems remotely downloads the gem package. Rubygems install all the gem packages in the/[RUBY root]/lib/Ruby/gems/[VER]/directory, this includes four directories: cache, Doc, gems, and specifications. Place the downloaded native gem package under the cache, and place the decompressed gem package under gems. When problems occur during installation, you can enter these directories, manually delete the problematic gem package, and then run the gem install [gemname] command again.

Rubygems command details:

Take the latest rubygems 1.8.16 as an example:

# View the version of rubygems
Gem-V

# Update and upgrade the rubygems Software
Gem update -- System

# Update all installed gem packages
$ Gem update

# Update the specified gem package
# Note: Gem update [gemname] does not upgrade the earlier version of the package. You can use gem install [gemname] -- version = [VER] instead.
$ Gem update [gemname]

# Install the specified gem package. The program first finds and installs the gem package from the local machine. If the gem package does not exist locally, it installs the package from the remote gem.
Gem install [gemname]

# Only install the gem package from the Local Machine
Gem install-L [gemname]

# Install the gem package remotely only
Gem install-R [gemname]

# Install the gem package, but do not install the relevant documentation.
Gem install [gemname] -- no-ri -- no-rdoc

# Install the gem package of the specified version
Gem install [gemname] -- version = [VER]

# Delete the specified gem package. Note that this command will delete all installed versions.
Gem uninstall [gemname]

# Delete a specified version of gem
Gem uninstall [gemname] -- version = [VER]

# View all installed gem packages on the local machine
Gem list

# List the gem packages with this keyword on remote rubygems.org (regular expressions are available)
Gem list-r keyword

# List all GMES lists on remote rubygems.org and save them to files.
Gem list-r> remote_gem_list.txt

# View all gem package documents and materials
Gem Server

# Displaying rubygem help
Gem help

# Examples of using rubygem commands
Gem help example

For more commands, see the official documentation: http://guides.rubygems.org/command-reference/

How to create your own Ruby gem package

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.