我正在尝试加载我最近创建的新gem,并且还没有任何代码,因为当我尝试加载仅用于测试时,会出现此错误。

我的代码:
我的宝石名字叫tracky。

tracky.gemspec

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

Gem::Specification.new do |spec|
  spec.name          = "tracky"
  spec.version       = Tracky::VERSION
  spec.authors       = ["Matheus Mendes"]
  spec.email         = ["[email protected]"]
  spec.summary       = %q{TODO: Write a short summary. Required.}
  spec.description   = %q{TODO: Write a longer description. Optional.}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = [`git ls-files -z`.split("\x0")]
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.7"
  spec.add_development_dependency "rake", "~> 10.0"
end


我的资料夹:

http://postimg.org/image/ve7nlyja7/


错误:

irb(main):001:0> require_relative 'lib/tracky'
LoadError: cannot load such file -- tracky/version
    from /home/matheus/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/matheus/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/matheus/Ruby/tracky/lib/tracky.rb:1:in `<top (required)>'
    from (irb):1:in `require_relative'
    from (irb):1
    from /home/matheus/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'


我在tracky的根目录中使用irb

最佳答案

lib/tracky.rb文件中,将require "tracky/version"更改为require_relative "tracky/version"

这是因为版本文件相对于lib/tracky.rb文件位于。我建议您为宝石依赖项保留require

检查this答案以了解requirerequire_relative之间的区别。

晚会晚了,但我想这可以帮助下一个人。我在面对同一问题时遇到了这个问题。

关于ruby-on-rails - Require_relative无法加载此类文件版本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31382288/

10-09 12:38