thedev
thedev

Reputation: 2886

Use ruby gem from git repo via Bundler

I have created a custom gem using bundler and checked it in a git repo.

custom_gem.rb

require "custom_gem/version"
require "custom_gem/custom_class"

module CustomGem
  # Your code goes here...
end

custom_class.rb

require 'typhoeus'

module CustomGem

  class CustomClass

    def self.custom_method
       #do stuff with typhoeus
    end

  end

end

Added it as a dependency to another project and installed it via bundler.

gem 'custom_gem', :git => '[email protected]:dir/repo.git'

After that I try to use it by calling

CustomGem::CustomClass.custom_method

and I get the following error:

uninitialized constant CustomGem::CustomClass

Any suggestions?

Might be a small thing but just starting out with ruby so any advice would be great.

Upvotes: 0

Views: 310

Answers (2)

Joshua Cheek
Joshua Cheek

Reputation: 31726

The file custom_gem.rb should be in lib/custom_gem.rb, and the file custom_class.rb should be in lib/custom_gem/custom_class.rb

lib/custom_gem/custom_class.rb
\_/ \________________________/
 |             |
 |              \_ comes from your code: `require "custom_gem/custom_class"`
 |
 |
  \_ comes from custom_gem.gempsec (the line `s.require_paths = ["lib"]`)

For more about the load path, file hierarchy and naming, check out this gem guide.

Upvotes: 1

Martin
Martin

Reputation: 7714

Two things to check :

  1. What happens when you run Bundle install (is the gem listed as installed ?)
  2. Did you require the gem properly (require 'custom_gem') ? Rails does a little magic there, but I'm not sure if you are in a rails application.

Upvotes: 1

Related Questions