在纯ruby脚本中使用gem(不是Rails)

时间:2014-08-18 23:43:29

标签: ruby bundler require

红宝石文件:

gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"
require "my-gem"

var1 = SomeGem::some_method123
puts var1

它说Could not find 'my-gem' (>= 0) among 330 total gem(s) (Gem::LoadError)。为什么不?我需要一个gem的特殊分支,并且不想克隆存储库。

1 个答案:

答案 0 :(得分:26)

使用bundler。在ruby脚本旁边创建一个Gemfile

Gemfile中,添加:

gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"

确保已安装捆绑包:

gem install bundler

并安装所需的宝石:

bundle install

现在只需在脚本顶部初始化bundler:

require 'rubygems'
require 'bundler/setup'

# require your gems as usual
require 'my-gem'