简体   繁体   English

要求宝石不起作用

[英]Require in gem don't work

I created gem with bundler and puts all my ruby files into '/lib' as documentation suggested. 我使用捆绑程序创建了gem,并根据文档建议将所有红宝石文件放入了“ / lib”中。 But I have a problem, after build the gem which "rake build" command and install (gem install pkg/gem) I can't use it because: 但是我有一个问题,在构建了“ rake build”命令并安装了gem之后(gem install pkg / gem),我不能使用它,因为:

LoadError: cannot load such file -- mygem/client LoadError:无法加载此类文件-mygem / client

this is cause because in main file i try to require 'mygem/client.rb' which is in lib/mygem/client.rb and it is doesn't work :/ 这是由于原因,因为在主文件中,我尝试要求在lib / mygem / client.rb中的“ mygem / client.rb”,但它不起作用:/

This is my gemspec: 这是我的gemspec:

# 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

Assuming your main file is named mygem.rb and is inside the lib folder, you should be able to require the file lib/mygem/client.rb with: 假设您的主文件名为mygem.rb且位于lib文件夹中,则您应该能够使用以下文件来要求文件lib/mygem/client.rb

require 'mygem/client'

Notice that I didn't use the .rb extension. 注意,我没有使用.rb扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM