Ruby

来自 Arch Linux 中文维基

Ruby 是一门专注于简洁和生产力的动态解释型开源编程语言。

安装 Ruby[编辑 | 编辑源代码]

要安装最新版本的 Ruby,安装 ruby 包。

要安装 IRB,安装 ruby-irb 包。

多版本[编辑 | 编辑源代码]

如果你要在同一系统中运行多个版本 (比如 2.0.0-p0 和 1.9.3-p392),最简单的办法就是使用 RVMchrubyAURrbenv 或者 asdf-vmAUR.

文档[编辑 | 编辑源代码]

要通过 ri 命令行工具访问文档,安装 ruby-rdoc(工具)和 ruby-docs(文档)。 你可以这样查询文档:ri Arrayri Array.pop 等等(很像手册页)。

JRuby[编辑 | 编辑源代码]

Ruby 的 Java 实现,JRuby 可通过 jruby 包安装。

RubyGems[编辑 | 编辑源代码]

RubyGems 是 Ruby 模块(称为 gems) 的包管理器,在某种程度上和 pacman 与 Arch Linux 的关系相当。它可通过 rubygems 包安装(rubygemsruby 的其中一个依赖项)。

设置[编辑 | 编辑源代码]

添加 $(ruby -e 'print Gem.user_dir')/binPATH 环境变量 来允许 RubyGems 被执行:

~/.profile
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"

这样而不是输入完整的路径对可执行的 gems 是必需的,尽管库工作并不需要修改你的路径。

为了允许当前 用户 安装 RubyGems,例如 你的用户帐号不允许安装到系统 RubyGems。,你可以设置 GEM_HOME 为本地路径:

$ export GEM_HOME=$HOME/.gem

你可能想把这个变量追加到 .profile 中:

~/.profile
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
export GEM_HOME=$HOME/.gem

使用 gem env 来查看当前的 RubyGems 环境:

$ gem env

用法[编辑 | 编辑源代码]

查看已安装有那些 gem:

$ gem list

获取某个 gem 的详细信息:

$ gem spec "gem_name"

默认情况下,gem listgem spec 开启了 --local 选项,导致 gem 只会在本地系统里进行搜索。这可以用 --remote 参数来覆盖。这样来搜索一个 mysql gem:

$ gem list --remote mysql

安装一个 gem:

$ gem install mysql

安装过程可以加快一点,如果你不需要本地的文档的话:

$ gem install mysql --no-document
注意: 这可以通过配置以下 ~/.gemrc 文件成为默认选项:
~/.gemrc
gem: --no-document

更新所有已安装的 gem:

$ gem update

安装每个用户或全系统的 gems[编辑 | 编辑源代码]

在 Arch Linux 下运行 gem 时,gems 默认是为用户安装的(安装到 ~/.gem/ruby​​/),而不是系统范围内(安装到 /usr/lib/ruby/gems/)。这被认为是 Arch 上管理 gems 的最好方式,因为否则它们可能会干扰 pacman 安装的宝石。

可以通过以 root 身份运行 gem 命令并附加 --no-user-install 标志以在系统范围内安装 gems。这可以被设置为默认通过在 /etc/gemrc(系统范围)或在 ~/.gemrc(用户,覆盖系统范围) 中用 --no-user-install 替换 --user-install

Bundler 通过将宝石打包到您的应用程序中来解决这些问题。请参阅下面关于使用 bundler 的部分。

Bundler[编辑 | 编辑源代码]

Bundler 帮助你指定你的项目需要使用哪些 gem,还有可选地指定哪个版本被需要。当声明恰当时,Bundler 会安装所有依赖的 gem (包括整个依赖树),后期检查之后还会打印出 log。Bundler 默认是安装 gem 到一个共享的路径,不过也可以指定直接安装到你的应用。当你项目运行的时候,Bundler 就提供每个 gem 正确的版本,即便安装的 gem 其实有多个版本。这需要有一点工作:应用需要以 bundle exec 命令运行,还有几行的 boilerplate 代码要被写在你的应用的可执行文件里。

安装 Bundler:

$ gem install bundler

Bundler 默认把 gem 安装在系统全局,这跟 Arch 上处理 gem 的方案正好相反。要解决这个问题,添加下面的配置到你的 ~/.bashrc:

export GEM_HOME=$(ruby -e 'print Gem.user_dir')

新建一个 bundle:

$ bundle init

然后编辑当前目录 (gem init 执行的目录)下的 Gemfile 添加你需要的 gem:

Gemfile
gem "rails", "3.2.9"
gem "mysql"

运行下面的命令安装需要的 gem 到 GEM_HOME:

$ bundle install

或者作为可选方案, 运行下面的命令把 gem 安装到工作路径的 .bundle:

$ bundle config set --local path '.bundle
注意: 命令 `bundle install --path .bundle` 已经过时。但如果你的 bundle 或 ruby 版本较低导致上条命令无效,可以选择尝试使用这条命令。

别忘了要编辑你主要的执行文件:

#!/usr/bin/env ruby

# "This will automatically discover your Gemfile, and make all of the gems in
# your Gemfile available to Ruby." https://bundler.io/rationale.html
require 'bundler/setup'

...

最后运行你的程序:

bundle exec main_executable_name.rb

Managing RubyGems using pacman[编辑 | 编辑源代码]

Instead of managing gems with gem, you can use pacman, or an AUR helper. Ruby packages follow the naming convention ruby-gemname.

This option provides the following advantages:

  • Gems are updated along with the rest of your system.
  • Installed gems are available system-wide, instead of being available only to the user who installed them.
注意: There are also tools integrating gem with pacman by automatically generating PKGBUILDs for specified gems: see Creating packages#PKGBUILD generators.

Quarry[编辑 | 编辑源代码]

Quarry is an opensource tool (GPL3 license) that allows to maintain rubygems binary repository for Arch Linux, as an easier alternative to building packages manually from the AUR. The source is hosted at github.

The repository is maintained by Arch developer anatolik at https://pkgbuild.com/~anatolik/quarry/, and is currently for the x86_64 architecture only. It contains many popular gems and new gems can be added upon request.

See Unofficial user repositories#quarry to enable it.

Then install required gem # pacman -S ruby-$gemname.

If you have general questions - send it at the project announcement https://bbs.archlinux.org/viewtopic.php?id=182729
If you have bugreports or code improvements - file at github https://github.com/anatol/quarry

See also[编辑 | 编辑源代码]