我用bundler创建了gem,并按照文档的建议将所有的ruby文件放到'/lib‘中。但是我遇到了一个问题,在构建了"rake build“命令和安装(gem install pkg/ gem )之后,我无法使用它,因为:
LoadError:无法加载这样的文件-- mygem/client
这是因为在主文件中,我试图要求“mygem/client.rb”,它位于lib/mygem/client.rb中,但它不工作:/
这是我的宝石规格:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'diggy/version'
Gem::Specification.new do |spec|
spec.name = "diggy"
spec.version = Diggy::VERSION
spec.authors = [""]
spec.email = [""]
spec.summary = %q{: Write a short summary, because Rubygems requires one.}
spec.description = %q{: Write a longer description or delete this line.}
spec.homepage = ""
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end
spec.files = `git ls-files -z`.split("\x0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
end
发布于 2017-02-15 14:16:39
假设您的主文件名为mygem.rb
,并且位于lib
文件夹中,那么您应该能够使用以下命令来要求文件lib/mygem/client.rb
:
require 'mygem/client'
注意,我没有使用.rb
扩展。
https://stackoverflow.com/questions/42250066
复制相似问题