按需安装宝石

我想在客户端安装gem(JSON),但只有在尚未安装(大约1.9个Ruby发行版捆绑JSON)的情况下。

我无法找到关于如何从gem help install完成的线索。 并且在gem install json了Ruby 1.9的Windows系统上运行gem install json (使用JSON捆绑)会导致结果

    ERROR:  Error installing json:
    The 'json' native gem requires installed build tools.

- 它试图安装它,忽略了宝石已经存在的事实。

我不能做bash技巧,如grep gem list输出,因为客户端可能是Windows。

那么,只有在系统中不存在的情况下,安装gem的多平台方式是什么?


这可能工作...

begin
  require "json"
rescue LoadError
  system("gem install json")
end

如果你不想要“json”,你可以从$ LOAD_PATH中删除它。

或者,把它作为一个班轮:

ruby -e 'begin; require "some_gem"; rescue LoadError; system "gem install some_gem"; end'

要问是否安装了宝石:

gem list --installed "^json$"

如果需要安装宝石:

ruby -e '`gem list -i "^json$"`.chomp=="true" or `gem install json`'

制作命令行脚本:

#!/usr/bin/env ruby
#
# Ruby script to install a gem if it's needed.
# This script first uses gem list to see if the
# gem is already installed, matching the exact name.
#
# If the gem is installed, then exit.
# If the gem is not installed, then install it.
#
# You can this script whatever you like;
# I call mine gem-install-fast because it's
# faster than re-installing a gem each time.
# 
# Example:
#
#    gem-install-fast json
#
name=ARGV[0] and `gem list -i "^#{name}$"`.chomp=="true" or `gem install #{name}`

要使用命令行脚本:

gem-install-fast json

gem update json

应该只在必要时安装在我的Windows 7系统上

C:Ruby193bin>gem update json
Updating installed gems
Updating json
Fetching: json-1.6.6.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed json-1.6.6
Updating multi_json
Fetching: multi_json-1.2.0.gem (100%)
Successfully installed multi_json-1.2.0
Gems updated: json, multi_json
Installing ri documentation for json-1.6.6...
Installing ri documentation for multi_json-1.2.0...
Installing RDoc documentation for json-1.6.6...
Installing RDoc documentation for multi_json-1.2.0...

C:Ruby193bin>gem update json
Updating installed gems
Nothing to update

C:Ruby193bin>
链接地址: http://www.djcxy.com/p/58641.html

上一篇: Install gem on demand

下一篇: Request a certificate from the certificate authority