Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Ruby Gem - Cannot Load Such File

Tags:

ruby

gem

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
like image 797
speersj Avatar asked Jan 06 '15 01:01

speersj


People also ask

How do I create a Ruby gem file?

A gemfile is automatically created when you start a new rails application. type rails new appName and then it will be generated automatically. It will also be populated with some gems.

How do I open a Ruby gem file?

Ruby GEM files can be installed using the "gem install GEMNAME [options]" command. You may also use the "gem list -r -d" command to list the gems installed on a network.

What is a Ruby Gem file?

A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.


1 Answers

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.

like image 93
matt Avatar answered Sep 21 '22 14:09

matt