2

I have a custom gem which is built on the following structure of files:

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

My gemspec file looks like this:

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

I'm able to build and install the gem without issues.

It also works on irb:

>> require 'searchcommon'

=> true

However, I have a script which will need this gem. The script includes the gem as follows:

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

The error I'm getting throws the following stack trace:

/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

The erroneous line is a "require 'commonfiles/rbbi'" inside lib/commonfiles/searchcommon.rb

The line inside rbbi.rb which has this error is "require 'scommon'" which is also present in the same folder.

I'm not able to understand why this wouldn't work in the script when I'm able to use the gem just fine with IRB.

I had a look at this question on SO but it didn't quite fix the same problem.

Any help would be appreciated.

Edit: Removed some confusing file names and replaced them.

Community
  • 1
  • 1
Nikhil
  • 3,042
  • 2
  • 16
  • 16

1 Answers1

3

You are trying to load scommon, but that file does not lie in the load path. You could use the 'full' path: require 'commenfiles/scommon'

Note, that this happens, because the file's directory ist not part of the load path. I recommend using require_relative instead (it does not use the load path): require_relative 'scommon'

J-_-L
  • 9,079
  • 2
  • 40
  • 37
  • Thank you for that! That fixed it. I used the 'full' path every where. I got a 'method missing' error for require_relative but I suspect that's because i'm on ruby 1.8.7. Thanks again. – Nikhil May 23 '12 at 10:06
  • Yes, require_relative is >= 1.9.2 – J-_-L May 23 '12 at 10:16