[英]How can I use a Ruby Gem file local to my project as opposed to a gem installed on the system?
[英]How do I build my own gem and use it in another project?
我正在嘗試構建我的第一個ruby gem,其中包含我想在項目中使用的一些類 - 為了這個問題,它被稱為test_service_api。
我使用“gem build test_service_api.spec”編寫並構建了gem。 然后我做了一個“gem install test_service_api-0.0.2.gem”。
spec文件是:
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/test_service_api/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Tyler Power"]
gem.email = ["tyler@xxx.com"]
gem.description = %q{}
gem.summary = %q{}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "test_service_api"
gem.require_paths = ["lib"]
gem.version = TestServiceApi::VERSION
end
我現在正試圖在一個小測試項目中使用gem,在測試項目Gemfile中我已經指定了對我構建的gem的依賴:
source 'https://rubygems.org'
gem 'sinatra'
gem 'test_service_api'
然后,我可以執行“捆綁安裝”,捆綁器正確查找所有依賴項並將它們放入供應商/緩存中:
Tylers-MacBook-Pro:Test tyler$ bundle install
Using rack (1.4.1)
Using rack-protection (1.2.0)
Using test_service_api (0.0.2)
Using tilt (1.3.3)
Using sinatra (1.3.2)
Using bundler (1.1.3)
Updating .gem files in vendor/cache
Your bundle is complete! It was installed into ./vendor/bundle
所以到目前為止我看起來都很好,我遇到的問題是,當我嘗試在測試項目中的源文件中需要gem時,它無法找到它:
require 'rubygems'
require 'sinatra'
require 'test_service_api'
get '/' do
res = ''
ENV.each do |k, v|
res << "#{k}: #{v}<br/>"
end
res
end
我的IDE抱怨“沒有文件加載”,如果我嘗試運行應用程序,我得到:
./app.rb:3:in `require': no such file to load -- test_service_api (LoadError)
from ./app.rb:3
我接近把頭發拉出來......有沒有人對如何解決這個問題有任何想法?
嘗試這個怎么樣:
require "rubygems"
require "bundler/setup"
而不僅僅是rubygems?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.