简体   繁体   English

通过Bundler从git repo使用ruby gem

[英]Use ruby gem from git repo via Bundler

I have created a custom gem using bundler and checked it in a git repo. 我使用bundler创建了一个自定义gem并在git repo中检查它。

custom_gem.rb custom_gem.rb

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

module CustomGem
  # Your code goes here...
end

custom_class.rb 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. 将其作为依赖项添加到另一个项目并通过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 未初始化的常量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 ?) 运行Bundle install时会发生什么(gem是否已列为已安装?)
  2. Did you require the gem properly (require 'custom_gem') ? 你是否正确地需要宝石(需要'custom_gem')? Rails does a little magic there, but I'm not sure if you are in a rails application. Rails在那里有点神奇,但我不确定你是否在使用rails应用程序。

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 文件custom_gem.rb应位于lib / custom_gem.rb中 ,文件custom_class.rb应位于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 . 有关加载路径,文件层次结构和命名的更多信息,请查看此gem指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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