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
- $ CD Hola
- $ Tree
- .
- ├ ── Hola. gemspec
- ── Lib
- └ ── Hola. Rb
2. write code
. Hola. Rb
[Ruby]View plaincopy
- % Cat lib/Hola. Rb
- Class Hola
- Def self. Hi
- Puts "Hello world! "
- End
- End
. Hola. gemspec
[Ruby]View plaincopy
- % Cat Hola. gemspec
- GEM: specification. New do | S |
- S. Name = 'hola'
- S. Version = '0. 0.0'
- S. Date = '2017-04-28'
- S. Summary = "Hola! "
- S. Description = "a simple hello world gem"
- S. Authors = ["Nick quaranto"]
- S. Email = '[email protected]'
- S. Files = ["lib/Hola. RB"]
- S. Homepage =
- 'Http: // rubygems.org/gems/hola'
- 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
- % 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 and use
[Ruby]View plaincopy
- % IRB
- > Require 'hola'
- => True
- > Hola. Hi
- 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
- $ Curl-u Tom https://rubygems.org/api/v1/api_key.yaml>
- ~ /. Gem/Credentials
- Enter host password for user 'Tom ':
Publish after setting
[Ruby]View plaincopy
- % Gem push hola-0.0.0.gem
- Pushing gem to rubygems.org...
- 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
- % Tree
- .
- ├ ── Hola. gemspec
- ── Lib
- ├ ── Hola
- │ ── Translator. Rb
- └ ── Hola. Rb
2. Code
lib/hola/translator.rb
[Ruby]View plaincopy
- % 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
[Ruby]View plaincopy
- % Cat lib/Hola. Rb
- Class Hola
- Def self. Hi (Language =: English)
- Translator = translator. New (language)
- Translator. Hi
- End
- End
- Require 'hola/translator'
. Hola. gemspec
[Ruby]View plaincopy
- % Cat Hola. gemspec
- GEM: specification. New do | S |
- S. Name = 'hola'
- S. Version = '0. 0.0'
- S. Date = '2017-04-28'
- S. Summary = "Hola! "
- S. Description = "a simple hello world gem"
- S. Authors = ["Nick quaranto"]
- S. Email = '[email protected]'
- S. Files = ["lib/Hola. RB", "lib/Hola/translator. RB"]
- S. Homepage =
- 'Http: // rubygems.org/gems/hola'
- 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
- % Mkdir Bin
- % Touch bin/Hola
- % Chmod A + x bin/Hola
2. Modify the executable file content
Bin/Hola
[Ruby]View plaincopy
- #! /Usr/bin/ENV Ruby
- Require 'hola'
- Puts Hola. Hi (argv [0])
Test
[Ruby]View plaincopy
- % Ruby-ilib./bin/Hola
- Hello World
- % Ruby-ilib./bin/Hola Spanish
- Hola Mundo
3. Modify the gemspec
[Ruby]View plaincopy
- % Head-4 Hola. gemspec
- GEM: specification. New do | S |
- S. Name = 'hola'
- S. Version = '0. 0.1'
- 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