Ben Downey
Ben Downey

Reputation: 2665

How to Require a Custom Gem

I'm writing a gem that depends on another gem I've created.

In my host gem, I'm requiring my gem as a dependency like this:

$:.push File.expand_path('../lib', __FILE__) 

Gem::Specification.new do |s| 
  s.require_path = "lib"
  s.files = Dir["lib/**/*"]
  s.test_files = Dir["spec/**/*"] 

  s.add_dependency "my_other_gem"
end 

My gemfile looks like this:

source "http://rubygems.org"

gem 'my_other_gem' path: '../my_other_gem', require: 'my_other_gem'

gemspec

And inside the host gem, I've got a class that requires my_other_gem:

require 'my_other_gem'

In my_other_gem, inside lib/my_other_gem.rb, I've got two more require classes. So it looks like this:

require 'my_other_gem/foo'
require 'my_other_gem/bar'

When I spin up IRB in the host app and run require 'my_other_gem', I get this error

LoadError: cannot load such file -- my_other_gem

When I'm playing in the my_other_gem directory and I spin up IRB, the same require 'my_other_gem' command does not error out. Everything runs normally. But for some reason I can't require my_other_gem when I'm in my host gem.

What step am I missing?

Upvotes: 1

Views: 871

Answers (1)

ouranos
ouranos

Reputation: 1234

How do you start irb? You need to run it in the bundler context with bundle exec.

I just tried and if I just run irb, I get the same error. But if I run bundle exec irb, it works.

Upvotes: 3

Related Questions