Making a Ruby Gem - Cannot Load Such File

。_饼干妹妹 提交于 2019-12-12 09:34:26

问题


I'm attempting to build a Ruby gem using the instructions at http://guides.rubygems.org/make-your-own-gem/. The following seems to work fine and a *.gem file is generated.

gem build mygem.gemspec

The following also appears to be successful (only if prefaced with sudo):

sudo gem install mygem-0.0.1.gem

However, when I attempt to require 'mygem' inside irb, I get the following error:

LoadError: cannot load such file -- mygem

I've seen similar errors around Stackoverflow but haven't been able to figure out what's going wrong in my specific situation. I am able to require other gems (not mine) without problems. My gem does show up in the output of gem list but it will not load with require.

FWIW I am using rbenv, which is brand new to me.

Here is the output of gem env:

  • RUBYGEMS VERSION: 2.4.5

    • RUBY VERSION: 2.1.5 (2014-11-13 patchlevel 273) [x86_64-darwin14.0]

    • INSTALLATION DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0

    • RUBY EXECUTABLE: /Users/speersj/.rbenv/versions/2.1.5/bin/ruby

    • EXECUTABLE DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/bin

    • SPEC CACHE DIRECTORY: /Users/speersj/.gem/specs

    • SYSTEM CONFIGURATION DIRECTORY: /Users/speersj/.rbenv/versions/2.1.5/etc

    • RUBYGEMS PLATFORMS:

    • ruby

    • x86_64-darwin-14

    • GEM PATHS:

      • /Users/speersj/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0

      • /Users/speersj/.gem/ruby/2.1.0

    • GEM CONFIGURATION:

      • :update_sources => true

      • :verbose => true

      • :backtrace => false

      • :bulk_threshold => 1000

    • REMOTE SOURCES:

      • https://rubygems.org/
    • SHELL PATH:

      • /Users/speersj/.rbenv/versions/2.1.5/bin

      • /Users/speersj/.rbenv/libexec

      • /Users/speersj/.rbenv/plugins/ruby-build/bin

      • /Users/speersj/.rbenv/shims

      • /Users/speersj/.rbenv/bin

      • /Library/Frameworks/Python.framework/Versions/3.4/bin

      • /usr/local/bin

      • /usr/local/sbin

      • /usr/local/heroku/bin

      • /usr/local/bin

      • /usr/bin

      • /bin

      • /usr/sbin

      • /sbin

      • /usr/local/bin

      • /usr/local/smlnj/bin

Gemspec:

Gem::Specification.new do |spec|
    spec.name        = 'mygem'
    spec.version     = '0.0.1'
    spec.date        = '2015-01-05'
    spec.summary     = "mygem" 
    spec.description = "Attempting to build a gem"
    spec.authors     = ["speersj"]
    spec.email       = # my email here
    spec.files       = ['lib/command.rb', 'lib/connection.rb']
    spec.homepage    = ''
    spec.license     = 'MIT'
end

回答1:


The spec.files entry of your gemspec doesn’t include the mygem.rb file, so that file won‘t be in the gem when it is built. Only files listed in this entry will be included in the final gem.

The simplest solution would be to just add mygem.rb to the array:

spec.files = ['lib/command.rb', 'lib/connection.rb', 'lib/mygem.rb']

This is a fairly simple fix, you might want to do something more flexible like using a Dir glob:

spec.files = Dir['lib/**/*.rb']

In fact the Rubygems guide suggests you do something like this (text is from the end of that section):

If you’ve added more files to your gem, make sure to remember to add them to your gemspec’s files array before publishing a new gem! For this reason (among others), many developers automate this with Hoe, Jeweler, Rake, Bundler, or just a dynamic gemspec.


Also, you really do need to fix your permissions problem, you shouldn’t need sudo to install gems into your own home directory.




回答2:


You can't use sudo to install a gem when using rbenv (or RVM), except for the "multi-user" or "system-wide" type installations which are specialized and very seldom what normal/regular users should be using.

sudo escalates your privileges to root, and root has no knowledge of the Rubies in a user's rbenv environment. As a result, root will use the default system Ruby, which will install the files there.

Instead, use a basic gem install, which will do the right thing.




回答3:


Make sure you added all modified files into github repo before build your gem And then install the build gem.



来源:https://stackoverflow.com/questions/27790712/making-a-ruby-gem-cannot-load-such-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!