【问题标题】:Require in gem don't work宝石中的要求不起作用
【发布时间】:2017-07-04 03:42:24
【问题描述】:

我使用 bundler 创建了 gem,并按照文档的建议将我的所有 ruby​​ 文件放入“/lib”。 但是我有一个问题,在构建了“rake build”命令并安装的gem(gem install pkg/gem)之后我不能使用它,因为:

LoadError: 无法加载此类文件 -- mygem/client

这是因为在主文件中我尝试要求 lib/mygem/client.rb 中的“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

【问题讨论】:

  • 您能否添加命令gem contents mygem(在本例中为gem contents diggy)和git status(在您的gems 开发目录中)的结果。

标签: ruby rubygems


【解决方案1】:

假设您的主文件名为 mygem.rb 并且位于 lib 文件夹内,您应该能够通过以下方式请求文件 lib/mygem/client.rb

require 'mygem/client'

请注意,我没有使用 .rb 扩展名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 2018-09-23
    • 2018-01-25
    • 1970-01-01
    • 2017-04-18
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多