GEM Package management usage in Ruby and Gem source Setup tutorial _ruby topics

Source: Internet
Author: User
Tags documentation

Proficiency in the use of RubyGems
RubyGems is a Ruby plug-in management system that allows you to easily install and manage Ruby library functions. All open source kits can be found on the rubygems.

# # #常见指令

    • Gem-v View the version of RubyGems
    • Gem Update--system upgrade RubyGems version
    • Gem install gem_name Install a plugin
    • Gem install-v x.x.x gem_name Install the specified version of the plug-in
    • The Gem list lists the installed kits
    • Gem Update gem_name updates a plugin
    • Gem Update updates all plugins
    • Gem Uninstall Gem_name remove a plugin

In addition, when installing Plug-ins, the RDoc and RI files for the plug-in are installed by default, and if you do not want to install them, you can use the--no-ri--no-rdoc parameters at installation time:

Gem Install Gem_name--no-ri--no-rdoc

You can also create a. GEMRC document under User directory, which reads as follows:
Gem:--no-ri--no-rdoc

The system will not install the RDoc and RI files by default.

# # #国内RubyGems镜像

If the server is in the country, installing the required gems will be an exceptionally painful experience, fortunately, now you can use the mirror taobao:

$ gem Sources--remove https://rubygems.org/
$ gem sources-a http://ruby.taobao.org/
$ gem sources-l

If the display:

Current SOURCES * * *

http://ruby.taobao.org

It means the change is successful, you can now install Rugy gems from the domestic mirror. For more information, refer to RubyGems mirroring

If you are using bundler to manage Ruby Gems, you can modify Gemfile:

SOURCE ' http://ruby.taobao.org/'

gem ' rails ', ' 3.2.2 '
...

# # #建立和分享Ruby Gems

According to the official profile:

Gem Update--system #Update to the latest RubyGems version
gem build Foo.gemspec #Build your gem and gem
push foo-1. 0.0.gem #Deploy your gem instantly

How to build your own RubyGems
# # #简单的示例:

For example, create a topico-0.0.1.gem:

### #建立文件夹

.
├──lib
│  └──topico.rb
└──topico.gemspec

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

### #编写代码 lib\topico.rb

Class Topico
 def Self.hello
  puts "Hello, rubygems!"
 End End

### #编辑GemSpec文件 Topico.gemspec

Gem::specification.new do |s|
 S.name    = ' Topico '
 s.version   = ' 0.0.1 '
 s.date
 = ' 2012-03-11 ' s.summary   = ' Greeting from Topico '
 s.description = ' Topico shows a greeting to RubyGems '
 s.authors   = ' Author Name ' 
   
    s.email    = ' username@username.com '
 s.files    = ["lib/topico.rb"]
 s.homepage  = ' http:// Rubygems.org/gems/topico ' End


   

Only the more common attributes are listed here.

### #编译生成gem

$ gem Build Topico.gemspec

The system prompts for information:

 Successfully built Rubygem
 name:topico
 version:0.0.1
 File:topico-0.0.1.gem

The folder structure can be viewed after compilation tree

.
├──lib
│  └──topico.rb
├──topico-0.0.1.gem
└──topico.gemspec

Note the newly generated Topico-0.0.1.gem

### #安装并测试gem

Install Topico-0.0.1.gem

$ gem install./topico-0.0.1.gem

The system will prompt:

Successfully installed topico-0.0.1
1 gem installed
installing RI documentation for topico-0.0.1 ...
Installing RDOC documentation for topico-0.0.1 ...

To test the use of IRB in IRB:

IRB (main):001:0> require ' Topico '
=> true
irb (main):002:0> Topico.hello
Hello,
rubygems! => Nil

### #发布到RugyGems网站

Set RubyGems username and password First:

$ curl-u username Https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
Enter host password for user ' username ':
 % total  % Received% xferd Average Speed   Current
                 dload Upload  Total  spent left  Speed
 0  0  0   0   0--:--:--0:00:02--:--:--  144

To publish after a successful setting:

$ gem Push Topico-0.0.1.gem
Pushing gem to https://rubygems.org
... Successfully registered Gem:topico (0.0.1)

Publish successfully so that everyone can use your rubygem.

# # #稍微复杂一些的示例:

Let's look at how to organize multiple Ruby files.

1. Directory structure

.
├──lib
│  ├──ext │ │  └──calculation.rb
│  └──topico.rb
└──topico.gemspec

2. Preparation of Gemspec

On the S.files line, modify:

 S.files    = ["Lib/topico.rb", "LIB/EXT/CALCULATION.RB"]

Re-gem build.

3. How to include the executable in the gem

(1) Under the plugin directory, create the Bin folder:

The build executes the piece, and the permissions are modified to be runnable.

$ mkdir bin
$ touch bin/greeting
$ chmod a+x bin/greeting

(2) Modify the contents of the executable file

#!/usr/bin/env Ruby

require ' Topico '
puts Topico.hello

(3) Modify Gemspec, add a row s.executables

 S.executables << ' greeting '

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.