Renra
Renra

Reputation: 5649

Cannot load a custom gem

I have a real newbie problem. I wrote a very small (one-file) library and I wanted to publish it in a gem so that I can use it in other projects modularly. I used the following gemspec:

Gem::Specification.new do |s|
  s.name        = 'symbolize-array'
  s.version     = '1.0.0'
  s.date        = '2013-11-22'
  s.summary     = "Symbolizes strings in arrays"
  s.description = ""
  s.files       = ["lib/array.rb"]
  s.homepage    =
    'https://github.com/renra/symbolize-array-ruby'
  s.license       = 'MIT'
end

I build the gem. Fine. I publish the gem. Fine. I install the gem from rubygems. Fine. But when I run irb and do require 'symbolize-array' I get:

LoadError: cannot load such file -- symbolize-array
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):4
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'

As you can see from the backtrace I use rvm. When I run 'gem environment' and I go to the gem path I can see my gem is installed just like the others. I can require the others, but I cannot require my gem. So I guess this is not a problem with the load path (I've seen questions that were answered like that) but maybe in the way I built the gem. Grateful for your ideas.

Upvotes: 0

Views: 912

Answers (1)

Neil Slater
Neil Slater

Reputation: 27197

You would need to require "array" to load your library, which may not work at all due to name clashes with other gems. When you install a gem, it just adds its library folder to the search path, and requires find files by name (without the .rb), they don't reference gem names.

Take a look at the naming conventions for gem components and also the link http://guides.rubygems.org/name-your-gem/

I suspect you should name the file "lib/symbolize_array.rb" and probably the gem name should be "symbolize_array" - ideally it would also implement a class or module SymbolizeArray in that case, but that is less important.

Upvotes: 3

Related Questions