Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with require in gem

I am trying to make a simple gem which has some modules. However, after I have built and installed the gem and try require it in a script I get an error:

from ...ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- my_gem/some_class (LoadError)

And

from ...ruby-1.9.3-p429/gems/my_gem-0.0.1/lib/my_gem.rb:2:in `<top (required)>'

Line 2 in my_gem.rb is require "my_gem/some_class.

As far as I understand (from this question) the problem seems to be that the file I require isn't found.

What is going on? Why can't I require my own files inside the gem?


The files I want to require is "next to" the version.rb.

#File Structure
#
# root
#   |->lib
#   |   |->my_gem.rb
#   |   |->my_gem
#   |   |    |->some_class.rb
#   |   |    |->some_class2.rb
#   |   |    |->version.rb

The SomeClass looks like this:

module MyGem
    class SomeClass
        def self.someMethod
        ...
    end
end

All I do in the script where I get the error is the following:

#!/usr/bin/ruby

require "rubygems"
require "my_gem"

Gemspec:

# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'my_gem/version'

Gem::Specification.new do |spec|
  spec.name          = "my_gem"
  spec.version       = MyGem::VERSION
  spec.authors       = ["My Name"]
  spec.email         = ["[email protected]"]
  spec.description   = %q{A simple Ruby gem that fails.}
  spec.summary       = %q{Failing gem for Ruby.}
  spec.homepage      = "http://some.website.com"
  spec.license       = "MIT"

  spec.files         = `git ls-files`.split($/)
  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.3"
  spec.add_development_dependency "rake"
end
like image 956
Automatico Avatar asked Aug 04 '26 15:08

Automatico


1 Answers

It looks like you have not added your class files to git, while your Gemspec file uses common pattern to specify files to be added to gem:

spec.files = `git ls-files`.split($/)

You have two options not to run into same trouble again:

  • Add all the files to version control before building a gem;
  • Use hand-written spec.files list in Gemspec.

Or, if you are using svn, you might want to change the spec.files to:

spec.files = `svn list`.split($/)

Hope it helps.

like image 121
Aleksei Matiushkin Avatar answered Aug 06 '26 09:08

Aleksei Matiushkin