2

我有一个基于以下文件结构的自定义 gem:

something/common/searchcommon.gemspec

something/common/lib/searchcommon.rb

something/common/lib/commonfiles/scommon.rb

something/common/lib/commonfiles/token_stream.rb

something/common/lib/commonfiles/rbbi.rb

我的 gemspec 文件如下所示:

Gem::Specification.new do |s|
  s.name        = 'searchcommon'
  s.version     = '0.0.0'
  s.date        = '2012-04-28'
  s.summary     = "Search Common classes"
  s.description = "Contains the common classes for search"
  s.authors     = ["some author"]
  s.email       = 'x@x.com'
  s.files       = ["lib/searchcommon.rb","lib/commonfiles/scommon.rb","lib/commonfiles/rbbi.rb","lib/commonfiles/token_stream.rb"]
end

我能够毫无问题地构建和安装 gem。

它也适用于 irb:

>> require 'searchcommon'

=> true

但是,我有一个需要这个 gem 的脚本。该脚本包含如下 gem:

#!/usr/local/bin/ruby
require 'rubygems'
require 'searchcommon'

我得到的错误引发以下堆栈跟踪:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- scommon (LoadError)
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/searchcommon-0.0.0/lib/commonfiles/rbbi.rb:1
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/searchcommon-0.0.0/lib/searchcommon.rb:1
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:60:in `gem_original_require'
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:60:in `require'
    from ./mapper.rb:3

错误的行是lib/commonfiles/searchcommon.rb中的“require 'commonfiles/rbbi'”

rbbi.rb 中出现此错误的行是“require 'scommon'”,它也存在于同一文件夹中。

当我能够将 gem 与 IRB 一起使用时,我无法理解为什么这在脚本中不起作用。

我在 SO 上查看了这个问题,但它并没有完全解决同样的问题。

任何帮助,将不胜感激。

编辑:删除了一些令人困惑的文件名并替换了它们。

4

1 回答 1

3

您正在尝试加载scommon,但该文件不在加载路径中。您可以使用“完整”路径:require 'commenfiles/scommon'

请注意,发生这种情况是因为文件的目录不是加载路径的一部分。我建议require_relative改用(它不使用加载路径):require_relative 'scommon'

于 2012-05-23T09:31:39.970 回答