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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With