简体   繁体   中英

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 => 'git@bitbucket.org: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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM