Creating Ruby Gem - unable to load such a file

I am trying to build a Ruby pearl using the instructions at http://guides.rubygems.org/make-your-own-gem/ . Everything seems to be working fine and a * .gem file is being created.

gem build mygem.gemspec 

The following also seems successful (only if it is preceded by sudo ):

 sudo gem install mygem-0.0.1.gem 

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

 LoadError: cannot load such file -- mygem 

I saw similar errors around Stackoverflow, but could not figure out what was going on in my specific situation. I can use other gems (not mine) without problems. My gem appears on the output of the gem list , but it will not load with demand.

FWIW I am using rbenv, completely new to me.

Here is the result of gem env :

  • VERSION OF ABROADS: 2.4.5

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

    • INSTALLATION INSTRUCTIONS: /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

    • SPECIAL SPECIALIZATION CACHE: /Users/speersj/.gem/specs

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

    • PLATFORMS OF REGIONS:

    • 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:

    • SHELL:

      • /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

      • /Ben

      • / 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 
+8
ruby gem
source share
3 answers

The spec.files your gemspec does not contain the mygem.rb file, so the file will not be in stone when it is created. Only the files listed in this entry will be included in the final stone.

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

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

This is a fairly simple fix, you can do something more flexible, for example using Dir glob :

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

In fact, the Rubygems manual suggests you do something like this (the text is at the end of this section):

If you added more files to your gem, be sure to remember adding them to the gemspecs array before publishing a new gem! For this reason (among other things), many developers automate this using Hoe , Jeweler , Rake , Bundler or just a dynamic gemspec .


In addition, you really need to fix the permission problem, you do not need sudo to install gems in your own home directory.

+13
source share

You cannot use sudo to set the gem when using rbenv (or RVM), except for installations of the "multi-user" or "system-wide" type, which are specialized and very rare that ordinary / regular users should use.

sudo extends your root privileges, and root does not know about Rubies in the rbenv user environment. As a result, root will use the standard Ruby system, which will install files there.

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

+2
source share

Make sure you add all the modified files to the github repository before building your gem. Then install the assembly.

0
source share

All Articles