自定义gem在irb中工作,而不是在ruby脚本中

时间:2012-05-23 06:28:23

标签: ruby rubygems irb

我有一个自定义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的脚本。该脚本包含如下宝石:

#!/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中的"要求' commonfiles / rbbi'

rbbi.rb中出现此错误的行"要求''" ,它也出现在同一个文件夹中。

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

我看了this question on SO,但它并没有解决同样的问题。

任何帮助都将不胜感激。

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

1 个答案:

答案 0 :(得分:3)

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

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