RubyGems 指南

Juanito FatasThinking about what to do.

RubyGems 是 Ruby 的套件管理工具,用來安裝各式各樣的第三方函式庫。

Ruby 1.9 內建 RubyGems,但盡量升級到最新版本(Bug 更少、功能更多)。

Ruby 寫成的函式庫通常會打包成 RubyGem 的形式(簡稱 Gem)。

譬如:Rails 是一個用 Ruby 寫的 RubyGem。

請看 Rubygems and Bundler 這個介紹 RubyGems 與 Bundler 的影片。

Gem 也有寶石的意思 💎 💎 💎 💎 💎 💎 💎 💎

RubyGems 提供一個可執行檔案,叫做 gem。以下簡單介紹每一個命令。

$ gem

$ gem help

$ gem -v
2.2.2

$ gem update --system

支援正規表示法(如下例 ^):

$ gem search ^rails

*** REMOTE GEMS ***

rails (4.1.1 ruby)
rails-3-settings (0.1.1 ruby)
rails-action-args (0.1.1 ruby)
rails-admin (0.0.0 ruby)
rails-admin-scaffold (0.0.5 ruby)
rails-adsense-helper (0.0.2 ruby)
rails-adsense_helper (0.0.1 ruby)
rails-ajax (1.0.0.20140320 ruby)
rails-alertify (0.3.2 ruby)
rails-alpha_numeric_validator (0.1.2 ruby)
rails-analytics (0.0.3 ruby)
rails-and-solid (0.9.1 ruby)
rails-angular-ui-router (0.0.1 ruby)
... (以下略) ...

https://rubygems.org (RubyGems 官方托管平台)搜索以 rails 開頭的 RubyGem。

每筆結果有「最新的版本資訊」與「使用何種的 Ruby 實作」寫成。

$ gem install jekyll
Fetching: sass-3.3.7.gem (100%)
Successfully installed sass-3.3.7
Fetching: jekyll-sass-converter-1.0.0.gem (100%)
Successfully installed jekyll-sass-converter-1.0.0
Fetching: coffee-script-source-1.7.0.gem (100%)
Successfully installed coffee-script-source-1.7.0
...()...
Fetching: fast-stemmer-1.0.2.gem (100%)
Building native extensions.  This could take a while...
Fetching: liquid-2.5.5.gem (100%)
Successfully installed liquid-2.5.5
Fetching: jekyll-2.0.2.gem (100%)
Successfully installed jekyll-2.0.2
30 gems installed

安裝 Jekyll 以及 Jekyll 所有相依的 RubyGems。

有一些 Gem 不是以純 Ruby 寫成,而是用 C 寫了擴充功能,則安裝時會看到:

Building native extensions.  This could take a while...

如上例的 fast-stemmer-1.0.2.gem

在安裝 RubyGem 時,會順道把該 RubyGem 的文件一起安裝,不想安裝(通常不會用到),可以在 gem install 時傳入 --no-document

會以字母 A-Z 順序列出本機已安裝的 RubyGem。

$ gem list

*** LOCAL GEMS ***

bigdecimal (1.2.4)
blankslate (2.1.2.4)
bundler (1.6.2)
...(略)...
timers (2.0.0)
toml (0.1.1)
yajl-ruby (1.1.0)

$ gem uninstall jekyll
Remove executables:
  jekyll

in addition to the gem? [Yn]
Removing jekyll
Successfully uninstalled jekyll-2.0.2

$ gem uninstall sass

You have requested to uninstall the gem:
  sass-3.3.7

jekyll-sass-converter-1.0.0 depends on sass (~> 3.2)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]
ERROR:  While executing gem ... (Gem::DependencyRemovalException)
    Uninstallation aborted due to dependent gem(s)

由於 jekyll-sass-converter 用到了 sass 這個 RubyGem,會詢問是否確定要移除。

$ gem server
Server started at http://0.0.0.0:8808
Server started at http://[::]:8808

會在 http://0.0.0.0:8808 列出所有已安裝 RubyGem 的文件。

$ gem fetch rake
Fetching: rake-10.3.1.gem (100%)
Downloaded rake-10.3.1

$ ls
rake-10.3.1.gem

$ gem unpack rake
Unpacked gem: 'path/to/this/directory/rake-10.3.1'
$ tree rake-10.3.1 -L 1
rake-10.3.1
├── History.rdoc
├── MIT-LICENSE
├── Manifest.txt
├── README.rdoc
├── Rakefile
├── bin
├── doc
├── lib
├── rakelib
└── test

5 directories, 5 files

$ gem environment

$ gem outdated

$ gem sources
*** CURRENT SOURCES ***

https://rubygems.org