RubyGems的功能类似于Linux下的apt-get。使用它可以方便第从远程服务器下载并安装Rails。

# 安装指定gem包,程序先从本机查找gem包并安装,如果本地没有,则从远程gem安装。
gem install [gemname]

# 仅从本机安装gem包
gem install -l [gemname]

# 仅从远程安装gem包
gem install -r [gemname]

Ruby’s package management system is known as RubyGems, and packages or modules
distributed using RubyGems are called “gems.” RubyGems makes it easy to install Ruby
software and can automatically manage complex dependencies between packages.
The frontend script for RubyGems is gem, and it’s distributed with Ruby 1.9 just as
irb and ri are. In Ruby 1.8, you must install it separately—see http://rubygems.org. Once
the gem program is installed, you might use it like this:
# gem install rails
Successfully installed activesupport-1.4.4
Successfully installed activerecord-1.15.5
Successfully installed actionpack-1.13.5
Successfully installed actionmailer-1.3.5
Successfully installed actionwebservice-1.2.5
Successfully installed rails-1.2.5
6 gems installed
Installing ri documentation for activesupport-1.4.4...
Installing ri documentation for activerecord-1.15.5...
...etc...
As you can see, the gem install command installs the most recent version of the gem
you request and also installs any gems that the requested gem requires. gem has other
useful subcommands as well. Some examples:

gem list # List installed gems

gem enviroment # Display RubyGems configuration information

gem update rails # Update a named gem
gem update # Update all installed gems

gem使用参考手册:http://guides.rubygems.org/

列出远程可安装的gems

如果你运行

gem query —remote # 快捷方式: gem q -R

你会看到所有远程服务器端gem包的详细列表。

简单的输出结果(已被严重简化):

*** REMOTE GEMS ***

 activerecord (0.8.4, 0.8.3, 0.8.2, 0.8.1, 0.8.0, 0.7.6, 0.7.5)
Implements the ActiveRecord pattern for ORM. BlueCloth (0.0.4, 0.0.3, 0.0.2)
BlueCloth is a Ruby implementation of Markdown, a
text-to-HTML conversion tool for web writers.
Markdown allows you to write using an easy-to-read,
easy-to-write plain text format, then convert it to
structurally valid XHTML (or HTML).
..........
.

2.3 搜索远程可安装的gems

如果你运行

 gem query --remote --name-matches doom
# 快捷方式: gem q -R -n doom

你会看到所有的远程服务器上与doom相匹配的gem包详细列表。

简单的输出结果:

*** REMOTE GEMS ***

 ruby-doom (0.8, 0.0.7)
Ruby-DOOM provides a scripting API for creating DOOM
maps. It also provides higher-level APIs to make map
creation easier.

2.4 安装远程gem

如果你运行 (如有必要,在根目录下运行)

gem install --remote progressbar
# 快捷方式: gem i -r progressbar

progressbargem就会安装在你的电脑上。要注意的是你不必指定它的版本,但是如果你一定要指定也可以。因为它默认会自动安装最新的版本。

gem ins -r progressbar-0.0.3

或者

gem ins -r progressbar --version '> 0.0.1'

这两种情况的输出结果都十分简单:

Attempting remote installation of ‘progressbar’Successfully installed progressbar, version 0.0.3

RubyGems允许你安装某个库的不同版本,并在代码中指定实际要使用的版本。

安装命令的其它有用参数是—gen-rdoc,用来生成gem包的RDoc API documentation,以及—run-tests,用来运行的单元测试(如果有单元测试的话)。

还要注意的是,当你远程安装gem包的时候,同时也将下载和安装所有规定的附属文件。你可以尝试着安装copland,然后它会弹出让你也接受log4r的提示(如果你还没安装它的话)。

2.5 查看已经安装的gem

如果你运行

gem specification progressbar
# 快捷方式: gem spec progressbar

你会看到’’progressbar’’ gem的详细内容。

简单的输出结果:

 --- !ruby/object:Gem::Specification
rubygems_version:"1.0\"
name: progressbar
version: !ruby/object:Gem::Version
version: 0.0.3
date: 2004-03-20 20:03:00.679937 +11:00
platform:
summary: "Ruby/ProgressBar is a text progress bar library for Ruby. It can
indicate progress with percentage, a progress bar, and estimated
remaining time."
require_paths:
- lib
files:
- sample/test.rb
- lib/progressbar.rb
- docs/progressbar.en.rd
- docs/progressbar.ja.rd
- ChangeLog
autorequire: progressbar
author: Satoru Takabayashi
email: satoru@namazu.org
homepage: http://namazu.org/~satoru/ruby-progressbar/

这些有趣的信息包括作者的情况,gem的版本和描述。

还有一些让RubyGems正确使用本gem的重要技术提示。它们包括gem包包含的所有文件,从哪里引入文件,以及默认需要的文件 (之后会有更多与此相关的内容)。

2.6 御载gem

如果我们使用完progressbar,可以将它御载。

gem uninstall progressbar

简单的输出结果:

Successfully uninstalled progressbar version 0.0.3

如果这个gem还安装了一个版本,gem命令会询问你想删除哪个版本。

如果有其它的gem需要于这个要被御载的gem,并且没有其它解决办法来替代这个gem,用户将会收到一个警告,并且可以取消此次御载。

2.7 列出所有已经安装的gems

非常简单,运行:

gem query --local
# 快捷方式: 'gem q -L'

2.8 本地和远程操作的注意事项

毫无疑问,你已经注意到  —remote出现在目前为止的多数命令行中。如果你不指定它们, gem会尝试本地和远程两个操作。例如:

gem ins rake # 尝试本地安装,如有必要,进行远程安装

gem list -b ^C # 列出所有以“C”开始的本地和远程gems

2.9 浏览所有已安装的gems及其文档

你可以运行自己的gem服务器。这就意味着其它人能(潜在地)安装你电脑上的gems。进而说明你可以在网络浏览器上查看已安装的gem。只需运行:

gem server

然后将页面指向:http://localhost:8808

只要你在安装gem的时候要求生成它的文档,你就能够浏览它们。

2.10 使用配置文件

如果你总是需要为安装的gem生成RDoc documentation文档,并且运行单元测试,你可以在配置文件(home 目录中的.gemrc文件)中指定这些命令行参数。

gem: --rdoc --test

使用配置文件还可以完成其它事情(RDoc 的参数,GEMPATH 的设置)。运行`gem help env`来查看详细内容。

2.11 其它特性

gem check —alien 会报告RubyGems 库中的流氓(不受管理的)文件。

gem check —verify progressbar 会检查已经安装的 ’’progressbar’’ gem对应其校检和是否有效。

参考:

http://article.yeeyan.org/view/130962/95500/

RubyGems使用的更多相关文章

  1. gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org

    在安装了rvm来管理多版本的ruby之后,想在不同环境下安装一些gems,结果gem install puma 之后,发现一次又一次失败. gem install 出现Errno::ECONNRESE ...

  2. rbenv Your user account isn't allowed to install to the system Rubygems

    Clone一个新Rails项目到Mac, bundle install 的时候遇到下面的提示 Fetching source index from http://rubygems.org/ Your ...

  3. 转 关于ruby gem无法连接到rubygems.org的解决方案

    为什么有这个? 由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败.所以你会与遇到 gem install rack 或 bundle ...

  4. 淘宝的ruby镜像已无人维护,使用ruby-china的RubyGems镜像

    淘宝的镜像已经无人维护了,参考 https://ruby-china.org/topics/29250 https://gems.ruby-china.org/ 使用新的镜像 $ gem source ...

  5. linux安装ruby ruby-devel rubygems bundler

    linux安装ruby ruby-devel rubygems yum install ruby ruby-devel rubygems 安装bundler gem install bundleror ...

  6. 各个平台 如何安装 Ruby 和 RubyGems

    原文地址:http://cloudfoundry-doc.csdn.net/frameworks/ruby/installing-ruby.html Last Updated: 2012-11-01 ...

  7. Linux 离线安装Rubygems详解

    很多时候我们会发现,真实的生成环境很多都没有外网,只有内网环境,这个时候我们又需要安装RubyGems,则不能提供yum命令进行在线安装了,这个时候我们就需要下载安装包进行离线安装.本文主要简单介绍如 ...

  8. 关于ruby gem无法连接到rubygems.org的解决方案

    RubyGems 镜像 - 淘宝网 为什么有这个? 由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败.所以你会与遇到 gem ins ...

  9. RubyGems系列之创建自己的gem

    转载请注明来源:https://www.cnblogs.com/zhanggui/p/9720818.html 一. 前言 我们可以在rubygems.org中下载安装他人创建的gem.现在,我们尝试 ...

随机推荐

  1. Android Studio快捷键快速入门

    调整,Settings->IDE Settings->Editor->Appearance->Show line numbers  显示代码行数Settings->IDE ...

  2. JAVA深入研究——Method的Invoke方法

    http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用 ...

  3. ios 可变参数(va_list,va_start,va_end)

    例如:UIAlertView的init方法中的otherButtonTitles:(NSString *)otherButtonTitles, ...等多个可变参数. ios实现传递不定长的多个参数的 ...

  4. Java——类比较器

    1.Product类 public class Product { private int pid; private String name; private double price; public ...

  5. Swift 语法须知

    什么是swift? swift是 2014 WWDC 发布的一款脚本语言. 使用Swift的好处: OC ARC    最大的困难  内存管理 而  swift  不用担心内存方面.   简洁 ,功能 ...

  6. javascript 布局 第20节

    <html> <head> <title>页面布局</title> <style type="text/css"> bo ...

  7. Codevs 1225 八数码难题

    1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的 ...

  8. QT5新手上路(1)安装

    这几天学了一下windows下的QT,也不算什么心得吧,就是谈一下我的做法.希望看到这篇随笔的菜鸟们略有所得,少走弯路. 闲话少说,先说安装.首先是选版本,我用的是qt-opensource-wind ...

  9. C++ map插入(insert)数据返回值

    例子: typedef boost::unordered_map<int, int> UserOnlineMap; UserOnlineMap userOnlineMap_; std::p ...

  10. 获取元素样式 currentStyle 和 getcomputedStyle

    场景 你要获取某一元素的样式,可是没有获取到,返回的值为undefined,可是有时候又能成功? 为什么? 因为,xx.stly.xxx 可以获取的样式信息,是dom元素style属性里的样式,对于通 ...