2012-11-25 56 views
10

我需要從普通的ruby腳本引用本地gem,而不安裝gem。對How to refer a local gem in ruby?的蹤跡,我試着用以下設置創建的Gemfile:如何從ruby腳本引用本地gem?

%w(
    custom_gem 
    another_custom_gem 
).each do |dependency| 
    gem dependency, :path => File.expand_path("../../#{dependency}", __FILE__) 
end 

和腳本如下所示:

require 'custom_gem' 
CustomGem::Do.something 

當我執行此:

bundle exec ruby script.rb 

我得到:

script.rb:1:in `require': cannot load such file -- custom_gem (LoadError) from script.rb:1:in `<main>' 

如果我離開了require 'custom_gem',我得到:

script.rb:3:in `<main>': uninitialized constant CustomGem (NameError) 

我甚至嘗試沒有捆綁,只是在腳本本身寫gem ... :path =>̣ ...,但沒有結果。是否有任何其他方式從ruby腳本引用自定義gems,而無需在本地安裝gems?

回答

10

確保您的寶石名稱相同的Gemfile(如custom_gem

# Gemfile 

source "https://rubygems.org" 

gem "custom_gem", path: "/home/username/path/to/custom_gem" 

不要忘了使用打捆

bundle install 

後實際安裝這種寶石,腳本應該準備使用bundle exec ruby script.rb

# script.rb 

require 'custom_gem' 
CustomGem::Do.something 
+0

是的,有拼寫錯誤,需要其他東西腳本中的寶石。謝謝! – tohokami