在gem中需要文件的问题

时间:2017-06-07 12:23:50

标签: ruby rubygems require

我的gem中有一个require问题。当我在main.rb中的用户需要require 'folder/first'时,它会抛出错误require': cannot load such file -- folder/first (LoadError)

所以我的问题是 - 为什么我不能在gem内部需要我自己的文件?

├── lib
     ├── folder
     │   ├── first.rb
     │   ├── second.rb
     │   ├── third.rb
     │   └── version.rb
     └── main.rb`

first.rb看起来像这样:

module MyGem
    class FirstClass
        def some_method
        ...
    end
end

main.rb看起来像这样:

require 'folder/first
module MyGem
   def run_first
      first = MyGem::FirstClass.new
      ....
   end
end

Gemspec:

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'relp/version'

Gem::Specification.new do |spec|

  ...

  spec.files         = `git ls-files`.split("\n")
  spec.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
  spec.require_paths = ["lib"]
  spec.bindir        = "exe"

  spec.required_ruby_version = '>= 2.0.0'

  spec.add_development_dependency "bundler", "~> 1.12"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "minitest", "~> 5.0"
end

`

0 个答案:

没有答案